$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?
$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 more modern suggestions that does not use this, such as:
$q = ucwords($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)))
It is important to note that the solutions provided here will not work with UTF-8 strings!
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει-υπέρ νωθρού κυνός";
$str = explode('-', mb_convert_case( $str, MB_CASE_TITLE ) );
$str = implode('-', array_map('mb_convert_case', $str, array(MB_CASE_TITLE, "UTF-8")) );
echo $str;
// str= Τάχιστη Αλώπηξ Βαφήσ Ψημένη Γη, Δρασκελίζει-Υπέρ Νωθρού Κυνόσ
look
function UpperCaseAfterDash($wyraz)
{
$rozbij = explode('-',$wyraz);
echo $rozbij[0].'-'.
ucfirst($rozbij[1]);
}
UpperCaseAfterDash("input-text");
Above function returns input-Text
If you need only uppercase letter after one dash for example with city names (Jastrzębie-Zdrój) it will be enough, but if you need more than one... , just count how many array elements (after explode in above code) exists, then use loop.
Greets,
Yes. ucfirst()
simply capitalized the first letter of the string. If you want multiple letters capitalized, you must create multiple strings.
$strings = explode("-", $string);
$newString = "";
foreach($strings as $string){
$newString += ucfirst($string);
}
function ucfirst_all($delimiter, $string){
$strings = explode("-", $string);
$newString = "";
foreach($strings as $string){
$newString += ucfirst($string);
}
return $newString;
}
You could do it with a regular expression callback method like this:
$q = preg_replace_callback('/\-([a-z]+)/g', create_function(
'$m', 'return "-" . ucfirst($m[1]);'
),$q)
Here's a generalized function that lets you capitalize after any character or string of characters. In the specific case the questioner asked about, the needle is the dash.
function capitalizeAfter( $needle, $haystack ) {
$haystack = str_replace( $needle . "a", $needle . "A", $haystack );
$haystack = str_replace( $needle . "b", $needle . "B", $haystack );
$haystack = str_replace( $needle . "c", $needle . "C", $haystack );
$haystack = str_replace( $needle . "d", $needle . "D", $haystack );
$haystack = str_replace( $needle . "e", $needle . "E", $haystack );
$haystack = str_replace( $needle . "f", $needle . "F", $haystack );
$haystack = str_replace( $needle . "g", $needle . "G", $haystack );
$haystack = str_replace( $needle . "h", $needle . "H", $haystack );
$haystack = str_replace( $needle . "i", $needle . "I", $haystack );
$haystack = str_replace( $needle . "j", $needle . "J", $haystack );
$haystack = str_replace( $needle . "k", $needle . "K", $haystack );
$haystack = str_replace( $needle . "l", $needle . "L", $haystack );
$haystack = str_replace( $needle . "m", $needle . "M", $haystack );
$haystack = str_replace( $needle . "n", $needle . "N", $haystack );
$haystack = str_replace( $needle . "o", $needle . "O", $haystack );
$haystack = str_replace( $needle . "p", $needle . "P", $haystack );
$haystack = str_replace( $needle . "q", $needle . "Q", $haystack );
$haystack = str_replace( $needle . "r", $needle . "R", $haystack );
$haystack = str_replace( $needle . "s", $needle . "S", $haystack );
$haystack = str_replace( $needle . "t", $needle . "T", $haystack );
$haystack = str_replace( $needle . "u", $needle . "U", $haystack );
$haystack = str_replace( $needle . "v", $needle . "V", $haystack );
$haystack = str_replace( $needle . "w", $needle . "W", $haystack );
$haystack = str_replace( $needle . "x", $needle . "X", $haystack );
$haystack = str_replace( $needle . "y", $needle . "Y", $haystack );
$haystack = str_replace( $needle . "z", $needle . "Z", $haystack );
return $haystack;
}