PHP capitalize after dash

Thanks to the delimiter parameter of ucwords, since PHP 5.4.32 and 5.5.16, it is as simple as this:

$string = ucwords($string, "-");

Updated Solution

As of PHP 5.5, the e modifier for preg_replace has been deprecated. The best option now is to use one of the suggestions that does not use this, such as:

$q = preg_replace_callback('/(\w+)/g', create_function('$m','return ucfirst($m[1]);'), $q)

or

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

Original Answer

You could use preg_replace using the e modifier this way:

$test = "durham-region";
$test = preg_replace("/(\w+)/e","ucfirst('\\1')", $test);
echo $test;
// Durham-Region

Tags:

Php