Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exploding UpperCasedCamelCase to Upper Cased Camel Case in PHP

Right now, I am implementing this with a split, slice, and implosion:

$exploded = implode(' ',array_slice(preg_split('/(?=[A-Z])/','ThisIsATest'),1));
//$exploded = "This Is A Test"

Prettier version:

$capital_split = preg_split('/(?=[A-Z])/','ThisIsATest');
$blank_first_ignored = array_slice($capital_split,1);
$exploded = implode(' ',$blank_first_ignored);

However, the problem is when you have input like 'SometimesPDFFilesHappen', which my implementation would (incorrectly) interpret as 'Sometimes P D F Files Happen'.

How can I (simply) get my script to condense 'P D F' to 'PDF'?

My qualification for when it should split would be to start at the first capital, and end one before the last, to accommodate the next word.

Yes, I know there are some ambiguities, like in 'ThisIsAPDFTest', which would be interpreted as 'This Is APDF Test'. However, I can't think of a "smart" way to avoid this, so it is an acceptable compromise.

like image 749
Austin Hyde Avatar asked Jul 18 '10 14:07

Austin Hyde


1 Answers

$input = "SomePDFFile";

$pass1 = preg_replace("/([a-z])([A-Z])/","\\1 \\2",$input);
$pass2 = preg_replace("/([A-Z])([A-Z][a-z])/","\\1 \\2",$pass1);
echo $pass2;

or, if you are very religious about having 1 statement:

preg_replace("/(([a-z])([A-Z])|([A-Z])([A-Z][a-z]))/","\\2\\4 \\3\\5",$input);

which is very ugly.

like image 75
mvds Avatar answered Sep 28 '22 16:09

mvds