Using preg_replace to make ordinals appear in all lower case
I have a column of address data that has ordinals displayed like this:
3Rd Floor, Cumbrian House
Room 223, 2Nd Floor
when I would like them to be displayed like this instead:
3rd Floor, Cumbrian House
Room 223, 2nd Floor
I'm trying to make use of the preg_replace function to swap out a
capitalised letter, that directly follows a number, with a lower case
letter instead. (I admit this is my first time using the preg_replace
function. I am however fine with preg_match and regular expressions).
So far I have:
$fcontents = file ('../addresses.csv');
$ordinalregex = '/(^.*\d+)([A-Z])/';
$correctordinal = '$1'.strtolower('$2');
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode("|", $line);
if (preg_match($ordinalregex,$arr[0])) {
echo preg_replace($ordinalregex,$correctordinal,$arr[0])."<br>";
}
}
But its not having the desired effect and outputs the line exactly as it
first appeared.
Thanks
No comments:
Post a Comment