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.
$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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With