mrlayance mrlayance - 1 year ago 152
PHP Question

PHP capitalize after dash

$q = durham-region;

$q = ucfirst($q);

$q = Durham-region;

How would I capitalize the letter after the dash (Durham-Region)? Would I have to split the two and capitalize?

Answer Source

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)


$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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download