「〇〇駅まで、徒歩○○分」、「〇〇駅まで、車で○○分」など入力のフォーマットがある程度決まった文字列をPHPの正規表現を使って英語にする。
public function englishStation($station_info){ if (preg_match('/(.+駅).*(徒歩|車で)([0-9]+)分/',$station_info)){ preg_match('/(.+駅).*(徒歩|車で)([0-9]+)分/',$station_info,$m); $isOkay = false; $stations = array('沼津駅'=>'Numazu Station','大岡駅'=>'Ooka Station','三島駅'=>'Mishima Station'); $transportation = array('車で'=>'by car','徒歩'=>'on foot'); foreach ($stations as $key => $value){ if(preg_match('/.*'.$key.'/',$m[1]) ){ $stationEn = $value; $isOkay = true; break; } } if ($isOkay==true){ $isOkay = false; foreach ($transportation as $key => $value){ if($key==$m[2]) { $way = $value; $isOkay = true; break; } } } if($isOkay){ return "$m[3] minutes $way to $stationEn"; } } return $station_info; } echo englishStation("JR東海道本線 沼津駅まで徒歩4分"); //4 minutes on foot to Numazu Station echo englishStation("三島駅まで車で13分"); //13 minutes by car to Mishima Station