开发者

How to capitalize the first letter after dash in PHP? [duplicate]

This question already has answers here: Make all words lowercase and the first letter of each word uppercase (3 answers) Closed 1 year ago. 开发者_如何学Go

This post was edited and submitted for review last month and failed to reopen the post:

Original close reason(s) were not resolved

$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)))


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)


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,


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;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜