Today, I was reading Id-PHP mailing list, and I found a question for it’s member. He is asking about to insert a space before a capital letters in a string.

Then, I come up with this solution.

$text = "ThisIsAnExampleOfPHPString";
echo preg_replace('/(\B[A-Z][^A-Z]*)/', ' \1', $text);

And the resut is “This Is An Example Of P H P String” — of course without the double quotes. Simple and neat.

2 comments… read them below or add one

Neki Arismi January 12, 2012 at 15:50 pm

Regex Made Confused *kabor*

ganda January 12, 2012 at 18:01 pm

Regex Made your tasks easier

Leave a Comment