Forum Moderators: coopster

Message Too Old, No Replies

Help with special Title Case

         

ro1960

3:54 pm on Apr 30, 2007 (gmt 0)

10+ Year Member



I use this code to display a field in title case:

echo ucwords(strtolower(htmlentities($city)));

However in some cases it does not produce the result I expect. For instance, if the field is "LOS ANGELES/WEST HOLLYWOOD" the output is:

Los Angeles/west Hollywood

How can I format it so it gives me:

Los Angeles/West Hollywood

Thank you.

henry0

4:35 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome on WebmasterWorld!

Not sure of your question
but to cap each word use
ucwords() [us2.php.net]

EDIT
you also may first make sure that you start with all lowercase() [us.php.net]then apply the second rule
/EDIT

willybfriendly

5:04 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may need to explode at the slash and then put it back together.

I suspect that ANGELES/WEST is being seen as one word, since there is no whitespace.

Perhaps replace the slash with a space, do the upper case thing, and then replace the slash

WBF

coopster

5:20 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Or you could use a regular expression.
$subject = 'LOS ANGELES/WEST HOLLYWOOD'; 
$subject = preg_replace("/\b([a-z])/e", "strtoupper('$1')", strtolower($subject));
print $subject;

The "e" modifier in the pattern tells the replacement routine to treat the value as PHP and render accordingly. What we are doing here is matching the first character following any word boundary and converting it to uppercase (after making the entire string lowercase in the first place).

willybfriendly

6:33 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Too Easy Coopster. ;)

WBF