Results 1 to 6 of 6

Thread: PCRE capitalization

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,419
    Thanks
    103
    Thanked 117 Times in 115 Posts

    Default PCRE capitalization

    Sort of a request for a script to add to my small collection. Is there a snippet of code out there that can capitalize a term in a string or the first letter in a sentence or everything in a string? I'll take anything.

    heh, I did a little search on this site about PCRE to see what other examples I could find and my name was the only one that came up well, asapcorp's name came up, but that thread doesn't deal with PCRE.

    If I am able to find the answer to this I will try and post it as well.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,419
    Thanks
    103
    Thanked 117 Times in 115 Posts

    Default

    kk, I found the answer. It wasn't easy to find either. I was not able to find the PCRE that I was looking for, however there are several built in PHP functions that deal with this issue.

    Lets say you wanted to replace all occurrences of the letters asdf with ASDF no matter the order they are found in in a string.
    Code:
    $string="this is a good fish.";
    $string = strtr($string,asdf,ASDF);
    echo "$string";
    //prints "thiS iS gooD FiSh."
    One problem is that you can't use this to match only "asdf" as opposed to the letters a,s,d,f irrespective of where they are located in the string, however for that you can always use
    Code:
    str_replace (fish,FISH,$string);
    //prints "this is good FISH."
    Just for completeness I was wondering about replacing the first letter in a string. Here is the code to use:
    Code:
    $string=ucfirst($string);
    There are many many other things that can and should be done using the built in string functions if you do not need complicated PCRE expressions although why I don't know It is just what they advise in the str_replace function at php.net.

    Some applications that I am thinking of that the built in PHP string functions could be used for include splitting a string into an array of sentences and using ucfirst() to capitalize the first letter of every sentence. You can use ucwords() for capitalizing the first letter in each word as in the case of lists of names or addresses. You can also use strtoupper() and strtolower() to change all of the letters to upper or lower case, but these almost do not seem worth mentioning.

    P.S. I still think that PCRE can be used for the preceding statements, but I no longer think it is the best idea.
    Last edited by james438; 08-11-2007 at 07:28 AM. Reason: spelling, completeness, and accuracy

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There are many many other things that can and should be done using the built in string functions if you do not need complicated PCRE expressions although why I don't know
    Regex is quite computationally expensive. The built-in functions are both more efficient and easier to read.
    P.S. I still think that PCRE can be used for the preceding statements, but I no longer think it is the best idea.
    I don't think it can. PCRE supports direct replace, but not modification based on the original string. It's possible in Python and ECMAScript by passing a function to the replace method, but this isn't possible in PHP -- since functions are represented as strings containing their names (ugh) PHP would be unable to distinguish a function name from an expression to be used directly.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,419
    Thanks
    103
    Thanked 117 Times in 115 Posts

    Default

    The reason that I suspect that PCRE can be used for capitalization (not that I have any reason to do so now) is because on PHP.net in the PCRE syntax page there is a snippet that says

    \cx
    "control-x", where x is any character
    The precise effect of "\cx" is as follows: if "x" is a lower case letter, it is converted to upper case. Then bit 6 of the character (hex 40) is inverted. Thus "\cz" becomes hex 1A, but "\c{" becomes hex 3B, while "\c;" becomes hex 7B.
    Not all that sure what hex characters are used for. I only recenly learned how to use octal characters.

    on a side note: I suppose that PCRE is quite expensive; I suspected as much, but as I was coding today I noticed that with one short PCRE command I could avoid several complicated lines of splitting and merging and editing arrays.

  5. #5
    Join Date
    Jul 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    strtoupper string $string //for all uppercase
    ucwords string $string )  //for first letter of all words uppercase
    ucfirst string $string )  // for first letter of the first word uppercase 
    i use ucwords A LOT throughout my site. Is there an easier/more efficient way to do this, or is ucwords adequate?

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,419
    Thanks
    103
    Thanked 117 Times in 115 Posts

    Default

    If I am understanding you correctly what you are asking for is a way to code where you won't need to type in ucwords() or strtoupper() for your scripts so often. If this is the case then you might want to look into using conditional loops like the following:

    Code:
    <br>
    <?php $terms=array(ae,be,ce,de,ee,fe,ge,he,ie,je,ke,le,me,ne,oe,pe,qe,re,se,te,ue,ve,we,xe,ye,ze);
    $t=0;$j=count($terms);
    while ($t<$j){
    if ($t&#37;2==0) $term[$t]=ucwords($terms[$t]);
    else $term[$t]=strtoupper($terms[$t]);
    echo"$term[$t]<br>";
    $t++;
    }
    ?>
    $j=count($link); counts the number of values in the array $link.
    $t++ increases the value of $t by one.

    basically the above will capitalize every even numbered value in your array and the odd numbered values in the array $terms will have the first letter of its value capitalized. But of course this all depends on what you need your script to do.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •