function mi2km($mystring) {
preg_match('/([^ ]*) (?=mph)/i', $mystring, $matches);
$kph=intval($matches[0]/0.621);
return preg_replace('/([^ ]*) mph/i', $kph . " kph", $mystring);
}
echo mi2km("Wind: N at 33 mph");
echo mi2km("Wind: South at 100 mph");
preg_replace('/(\d+) mph/ie', "round((int)\\1/0.621) . ' kph'", $str);
The bonus of the following say is that as long as the string ends with *** mph it will always work, no matter what is in front of it.
Open in new window