As part of a simple URL cleaning/slugging function I'm extending, I need to make conversions such as the following:
Original Converted
---------------------------------------------
USAMarch UsaMarch
FETExaminations FetExaminations *
AnotherABBRString AnotherAbbrString
LastONE LastOne
(These are just examples, except for the second, which is what got me to doing this in the first place.)
I'm assuming I need to use preg_replace_callback
to determine the position of any abbreviations and case them appropriately.
I haven't the foggiest of where to begin. Does anyone know what I can do to get this right?
Update
Okay, I have this so far:
$input = preg_replace_callback("~([A-Z])([A-Z]+)([^a-z])([a-z]|)~",
function ($captures) {
return $captures[1].strtolower($captures[2]).$captures[3].$captures[4];
},
$input);
Unfortunately, however, it doesn't work with strings where the abbreviation is last:
This Becomes
---------------------------------------------
LastONE LastOnE
So I'm checking the end incorrectly, I assume. Also, this attempt isn't recursive. How would I do that?