15
$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?

Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25
mrlayance
  • 655
  • 9
  • 24

9 Answers9

25

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
Kelly
  • 40,173
  • 4
  • 42
  • 51
  • Note: Using preg_replace_callback can lead to a white screen as in my case (unexpected T_FUNCTION). Thanks @Kelly for providing the other options! – Avatar Nov 18 '14 at 08:55
  • Watch out, using the preg_replace with a word that contains Umlaute, e.g. `$test = "wärme-test";` leads to results like "WäRme-Test". In other words, the letter after the Umlaut also gets capitalized. – Avatar Nov 18 '14 at 11:58
  • Note, This does not only capitalize after a dash, but after EVERY non-word-character, even the first character of the string is capitalized. – rubo77 Mar 23 '21 at 18:55
  • `create_function` is also deprecated in PHP 7.4 – rubo77 Mar 23 '21 at 18:55
  • preg_replace is a poor solution and unsuitable from a high-scale point of view. – Prabhu Nandan Kumar Jan 11 '23 at 11:15
16

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, "-");
Alexander Jank
  • 2,440
  • 2
  • 18
  • 19
9

A one-liner that doesn't envolve using the e PCRE modifier:

$str = implode('-', array_map('ucfirst', explode('-', $str)));
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
1

another oneliner:

str_replace(' ','',ucwords(str_replace('-',' ',$action)))
Flion
  • 10,468
  • 13
  • 48
  • 68
  • just a caution, this could affect strings that both contain a space and a dash, but for single words, this is also good. – bonbon.langes Oct 21 '15 at 14:46
0

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= Τάχιστη Αλώπηξ Βαφήσ Ψημένη Γη, Δρασκελίζει-Υπέρ Νωθρού Κυνόσ
Tim Groeneveld
  • 8,739
  • 3
  • 44
  • 60
0

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,

0

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;
}
Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
0

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)
Billy Moon
  • 57,113
  • 24
  • 136
  • 237
0

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;

}
Ben Shoval
  • 1,732
  • 1
  • 15
  • 20