SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    sydney
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with special Title Case

    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.

  2. #2
    SitePoint Zealot dawgbone's Avatar
    Join Date
    Dec 2003
    Location
    Brampton
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason why it is doing that is because Angeles/west looks like one word to strtolower.

    There's probably a better way, but try breaking up the text and putting it into an array, then running strtolower, then putting the string back together.

  3. #3
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds like the way ucwords interpretes a slash doesn't match the way you need it. So, you'll have to find a way to get around this. For example, if cities( are seperated (if at all) by only a slash, you can split them, perfom the case change, and join them back.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    sydney
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    play around a bit with strstr() function.

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str=str_replace('/',' <slash/> ',$city);
    $str=ucwords($str);
    echo 
    str_replace(' <slash/> ','/',$str); 
    Saul

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    sydney
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This gives an odd result:

    Los/Angeles/West/Hollywood

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    does it?!
    Tested the code and it's works as expected. What does the string look like before?
    ie:
    Los angeles/west Hollywood
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid ro1960 has caught my original post which was there only for a few seconds, that would give such results.
    Saul

  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    fair enough
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ro1960 View Post
    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.
    lol.. you got some funny replies on this question..

    Code:
    $cityparts = explode("/", $city);
    $city = ucwords(strtolower($cityparts[0]))."/".ucwords(strtolower($cityparts[1]));
    $city = trim($city,"/"); // strips the trailing /
    
    echo htmlentities($city);
    let me know how that works for you


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
  •