PHP script to strip all CAPS and then only Capitalize first letter of each word

Discussion in 'PHP' started by pmf123, Jun 25, 2008.

  1. #1
    I'm looking for the PHP script to strip all CAPS and then only Capitalize first letter of each word...

    ie. "THIS IS A TEST" becomes "This Is A Test"

    Can anyone help?
     
    pmf123, Jun 25, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    $words = ucwords(strtolower($words));
     
    Danltn, Jun 25, 2008 IP
  3. pmf123

    pmf123 Notable Member

    Messages:
    1,449
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    215
    #3
    Thank you for your prompt response.
     
    pmf123, Jun 25, 2008 IP
  4. pmf123

    pmf123 Notable Member

    Messages:
    1,449
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    215
    #4
    What would the modification be if some of the values are in the format "WESTON-SUPER-MARE" or "BARROW-IN-FURNESS" using "-" instead of space (and you want to retain those "-"s) but want to apply the capitalization rule?
     
    pmf123, Jun 25, 2008 IP
  5. pmf123

    pmf123 Notable Member

    Messages:
    1,449
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    215
    #5
    Ok just found a function for this elsewhere, so posting it here incase anyone else needs it:

    <?
    function Capitalize($name) {
    $name = strtolower($name);
    $name = join("'", array_map('ucwords', explode("'", $name)));
    $name = join("-", array_map('ucwords', explode("-", $name)));
    $name = join("Mac", array_map('ucwords', explode("Mac", $name)));
    $name = join("Mc", array_map('ucwords', explode("Mc", $name)));
    return $name;
    }
    echo Capitalize('BURY-ON-THAMES') . '<br>';
    echo Capitalize('ISLE OF WIGHT') . '<br>';
    echo Capitalize('MCDONALD') . '<br>';
    ?>
     
    pmf123, Jun 25, 2008 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    You don't need the function. The above should work fine, even if it contains dashes.

    Peace,
     
    Barti1987, Jun 25, 2008 IP