3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sepNameDist($place){ $placeArr = explode("(",$place); if(count($placeArr)>1){ $array['name'] = ucwords(trim($placeArr[0])); $clean = strtolower(str_replace(" ","",trim(str_replace(")","",$placeArr[1])))); $matches = (preg_split('#(?<=\d)(?=[a-z])#i', $clean)); $num = $matches[0]; $txt = $matches[1]; if($num < 1 and (trim(strtolower($txt)) == "km" or (trim(strtolower($txt)) == "kms"))){ $num = $num*1000; $txt = "m"; $array['distance'] = $num." ".$txt; } else{ $array['distance'] = $num." ".$txt; } return $array; } else{ $placeArr = explode(" ",trim($place)); return $placeArr; } } print_r(sepNameDist("Birla Mandir 30 Km"));

preferences:
65.38 ms | 402 KiB | 5 Q