PHP capitalize after dash

后端 未结 9 1596
长情又很酷
长情又很酷 2021-01-17 13:46
$q = durham-region;

$q = ucfirst($q);

$q = Durham-region;

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

相关标签:
9条回答
  • 2021-01-17 14:29

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

    $str = implode('-', array_map('ucfirst', explode('-', $str)));
    
    0 讨论(0)
  • 2021-01-17 14:30

    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, "-");
    
    0 讨论(0)
  • 2021-01-17 14:30

    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)
    
    0 讨论(0)
  • 2021-01-17 14:31

    another oneliner:

    str_replace(' ','',ucwords(str_replace('-',' ',$action)))
    
    0 讨论(0)
  • 2021-01-17 14:38

    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 讨论(0)
  • 2021-01-17 14:45

    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 讨论(0)
提交回复
热议问题