PHP capitalize after dash

后端 未结 9 1665
长情又很酷
长情又很酷 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: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;
    
    }
    

提交回复
热议问题