How to uppercase first letter after a hyphen, ie Adam Smith-Jones

Is this ok ?

    function to_upper($name)
    {
        $name=ucwords($name);
        $arr=explode('-', $name);
        $name=array();
        foreach($arr as $v)
        {
            $name[]=ucfirst($v);
        }
        $name=implode('-', $name);
        return $name;
    }
    echo to_upper("adam smith-jones");

What do you think about the following code ?

mb_convert_case(mb_strtolower($value), MB_CASE_TITLE, "UTF-8");

Please note that this also handles accented characters (usefull for some languages such as french).


$string = implode('-', array_map('ucfirst', explode('-', $string)));

Other way:

<?php

$str = 'adam smith-jones';

echo preg_replace("/(-)([a-z])/e","'\\1'.strtoupper('\\2')", ucwords($str));

?>

Tags:

Php