1

I'm trying to capitalize every letter after space and dash. Obviously, capitalizing every letter after a space isn't a problem:

$string = preg_replace('/[^a-zA-Z-\s]/s', '', $string);
$string = ucwords(strtolower($string));

does the trick.

However, I can't find a way to capitalize every letter after a dash, although this regex seems to match every letter after a dash (if i trust the answer).

Any help is appreciated!

I also tried:

$string = preg_replace('#\b[a-z0-9-_]+#i', strtoupper("$0"), $string);

without success...

Community
  • 1
  • 1
fkoessler
  • 6,932
  • 11
  • 60
  • 92
  • 1
    I've made a javascript version once upon a time :) hope it helps: `str = str.toLowerCase().replace(/^(.)|\s(.)|\#(.)/g, function (letter) { return letter.toUpperCase(); });` – pilavust Aug 10 '12 at 10:02

1 Answers1

8
preg_replace_callback('/(?<=( |-))./',
                      function ($m) { return strtoupper($m[0]); },
                      $string);

/(?<=( |-))./ is "any character (.) preceded by ((?<=)) a space or dash (( |-))".

deceze
  • 510,633
  • 85
  • 743
  • 889