I think you need to use preg_replace_callback as I'm not sure you can replace based on the part you matches in normal preg_replace. What you need to do is use a callback function that takes the matches array and processes it (you could also just use preg_match and then work with the matches seperetaly).
This example works (although it uses an anonymous function for the callback, which requires PHP 5.3.0+):
function fixOrdinals($string) {
return preg_replace_callback("/[0-9][A-Z]/",
function ($matches) {
return strtolower($matches[0]);
},
$string
);
}
echo fixOrdinals("3Rd Floor, Cumbrian House")."\n";
echo fixOrdinals("Room 223, 2Nd Floor")."\n";
echo fixOrdinals("4Th Room, 2Nd Floor")."\n";
Output:
3rd Floor, Cumbrian House
Room 223, 2nd Floor
4th Room, 2nd Floor