- print_r: documentation ( source)
- ucwords: documentation ( source)
- str_replace: documentation ( source)
- explode: documentation ( source)
- trim: documentation ( source)
- preg_split: documentation ( source)
- strtolower: documentation ( source)
<?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"));