DevHints

PHP: Proper Case Function

Posted on: October 21, 2006

PHP doesn’t have a built in proper case string function, though it does have upper and lower case functions. This isn’t too big of a problem though, since we can build one that acts just like we want anyways!

If you just have a short phrase or word that needs to have the very first letter capitalized, we’ve got a nice and easy job. Here’s the code:

$string = strtolower($string);
$string = substr_replace($string, strtoupper(substr($string, 0, 1)), 0, 1);

That might look a bit messy if you don’t like putting things together all at once, so here’s the jist: On the first line we make the whole string lowercase since we don’t know if there were capital letters where they shouldn’t be. The most inner part of the second line (substr) pulls the first character from our string. Moving outwards, the next function makes it uppercase, then finally, we replace the first letter of our string with the new uppercase letter.

Now what if we wanted every word in the phrase capitalized? Simple.

$words = explode(” “, $string);
for ($i=0; $i<count($words); $i++) {
$s = strtolower($words[$i]);
$s = substr_replace($s, strtoupper(substr($s, 0, 1)), 0, 1);
$result .= “$s “;
}
$string = trim($result);

Here we split the phrase up based on each space character and then did the same as we did above, only for each word. Then we built it back together word by word.

UPDATE:  I didn’t realize it, but the ucwords function in PHP will do just this.

ucwords(strtolower($string));

Advertisements

6 Responses to "PHP: Proper Case Function"

I am looking to get each word capitalised and remove the hyphens in the URL, I have got it to work on the first letter, what do I need to change to the following ?

$url=”http://”.$_SERVER[‘HTTP_HOST’].$_SERVER[‘REQUEST_URI’]; // gets the url of the page
$area = basename($_SERVER[‘REQUEST_URI’], “.php”); // get current page name : supposing filetype .php
$area = strtolower($area); $area = substr_replace($area, strtoupper(substr($area, 0, 1)), 0, 1); // capitalising first letter

Any help would be much appreciated.

Thanks for posting

In fact php has an “ucfirst” function that makes uppercase the first letter in a lowercase string:

ucfirst(strtolower($string));

modified to be a function and defined $result = ”;

function str_to_proper($string)
{
$result = ”;
$words = explode(” “, $string);
for ($i=0; $i<count($words); $i++) {
$s = strtolower($words[$i]);
$s = substr_replace($s, strtoupper(substr($s, 0, 1)), 0, 1);
$result .= "$s ";
}
$string = trim($result);
return $string;
}

Guys, you have to simplify things. Use preg_replace

preg_replace(‘/((^[a-z])|( +[^&#a-z]?[a-z]{1,1}))/i’, strtoupper($1), $YOUR_VARIABLE);

This will search for and replace the first letter of the string OR the first letter of each word with its uppercase version. It will also ignore HTML special entities like & and consider words inside parenthesis.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: