View Full Version : PCRE capitalization
james438
08-11-2007, 12:45 AM
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 :p 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.
james438
08-11-2007, 06:31 AM
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.
$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
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:
$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.
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 knowRegex 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.
james438
08-11-2007, 11:15 PM
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.
superjadex12
08-12-2007, 02:15 AM
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?
james438
08-12-2007, 03:06 AM
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:
<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%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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.