$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?
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
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, "-");
A one-liner that doesn't envolve using the e
PCRE modifier:
$str = implode('-', array_map('ucfirst', explode('-', $str)));
another oneliner:
str_replace(' ','',ucwords(str_replace('-',' ',$action)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With