PDA

View Full Version : Help with special Title Case


ro1960
04-30-2007, 04:53 PM
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.

rafiki
04-30-2007, 05:06 PM
implode? or is it explode?
basically

<?php
$city = '';//however you find your city
$city = implode("/", $city);
$city = ucwords(strtolower(htmlentities($city)));
?>

ro1960
04-30-2007, 06:32 PM
How about this?


$cityparts = explode("/", $city);
$city = ucwords(strtolower($cityparts[0]))."/".ucwords(strtolower($cityparts[1]));

echo htmlentities($city);


It correctly prints Los Angeles/West Hollywood

But how do I write an if statement to only apply to $city containing a slash? Otherwise it adds a slash at the end of other city names like this:

New York/

I am not familiar with string manipulation.

Thank you again.

kbluhm
04-30-2007, 06:47 PM
Untested, but should work... pretty straight-forward:
<?php

function city_name_format( $name )
{
$name = str_replace( '/', ' / ', $name );
$name = ucwords( strtolower( $name ) );
$name = str_replace( ' / ', '/', $name );
return htmlentities( $name );
}

// usage
$city = city_name_format( $city );

?>

NancyJ
04-30-2007, 07:01 PM
how about

echo str_replace("/ ", "/", ucwords(strtolower(htmlentities(str_replace("/", "/ ", $city)))));

kbluhm
04-30-2007, 07:52 PM
Holy crap... same thing I have.

iLLin
04-30-2007, 08:26 PM
Yea in one line ;)

kbluhm
04-30-2007, 09:30 PM
Why take the same thing and rewrite it? ;)

People do realize these are examples, don't they? They are put into multiple lines to avoid the question 'how does it do that?'... If they see how it works, maybe then they will learn something and avoid questions down the road. ;)

And good luck when trying to go through a script you wrote a year ago while thinking it'd be fancy to squeeze 10 lines of code into one. ;)

NancyJ
04-30-2007, 09:53 PM
Why take the same thing and rewrite it? ;)


yeah, why take my code and rewrite it? Because what your originally posted sure as hell wasn't what is there now.

Its not a competition, we're all trying to do the same thing - help the OP. It doesnt matter whose solution the OP chooses or who gets there first, as long as they get the help they wanted.

iLLin
04-30-2007, 10:08 PM
Wow he took that personal... Didn't mean to make it personal and for you to get all defensive, my appologies. But if I can take 10 lines of code and turn it into 1, them I'm there. Especially if it will speed up my code. But I'm the type of person that is always searching for the best and fastest way to write code. Other people like to block it out as much as possible.

For instance


if($color == black) {
$color = red;
} else {
$color = black;
}


A simple alternating color if statement. But I write the same thing like this:


$color = $color == black ? red : black;


Faster? I honestly don't know.
Simpler? Hell yes.
File size? Smaller.
Cleaner? I think so.

But I'm that type of person, didn't mean to come off as a personal attack on you. :)

kbluhm
05-01-2007, 12:37 AM
yeah, why take my code and rewrite it? Because what your originally posted sure as hell wasn't what is there now.
What are you kidding me?

Its not a competition, we're all trying to do the same thing - help the OP. It doesnt matter whose solution the OP chooses or who gets there first, as long as they get the help they wanted.
Good point... I'm going to have a heck of a lot of posts on here just rewriting previous code snippets, nesting functions inside of functions inside of functions inside of functions inside of functions... and then say "how about that too?". Oh how smart I will look.

We're not talking ternary operators here.

NancyJ
05-01-2007, 07:44 AM
What are you kidding me?

When I posted your code was so full of if, TRUEs and FALSEs that I didnt even bother to read it, it was very clear from a single glance that my solution was different. And yet, 2 hours later, you've editted it to be the same process as mine, fine whatever, if your life is so small you need to do that I really pity you but its not skin of my nose, but then to accuse ME of copying YOU?
I wonder if moderators can see what posts were before they were editted.

ro1960
05-01-2007, 01:54 PM
Thanks Nancy J.

This works perfectly and is indeed compact.

But just for my eduction, could you comment the various functions in your code so I can go beyond the cut-and-paste?

Also there may be instances where there is a hyphen in the city name. Two-word (or more) cities have a hyphen in French like Saint-Michel or Saint-Laurent-Du-Var.

PS: Sorry guys, I didn't mean to start a passionate discussion! :-)

iLLin
05-01-2007, 03:42 PM
When I posted your code was so full of if, TRUEs and FALSEs that I didnt even bother to read it, it was very clear from a single glance that my solution was different. And yet, 2 hours later, you've editted it to be the same process as mine, fine whatever, if your life is so small you need to do that I really pity you but its not skin of my nose, but then to accuse ME of copying YOU?
I wonder if moderators can see what posts were before they were editted.

LOL, He did change his post. Now thats funny. Yea before it was like 12-15 lines with lots of TRUE/FALSE...

NancyJ
05-01-2007, 03:42 PM
The only function I added was str_replace (twice) Once to add a space after the slash and another to take it away again when you're done with the title case stuff. To do the same for hyphen you just need to use arrays for the search and replace parameters, so the first element in the search gets replaced by the first element in the replace and the second by the second - if you get my meaning.

echo str_replace(array("/ ","- "), array("/", "-"), ucwords(strtolower(htmlentities(str_replace(array("/","-"), array("/ ","- "), $city)))));

CFMaBiSmAd
05-01-2007, 03:49 PM
LOL, He did change his post. Now thats funny. Yea before it was like 12-15 lines with lots of TRUE/FALSE...All the more reason why the edit time limit only needs to be 30 minutes or so to allow typos and inadvertently posted sensitive information to be changed by the OP.