The title says it all.
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.