我在我的web应用程序中使用Google方向API,有没有办法缩短Google提供的方向描述?
我的意思是,例如,
Take the 2nd right.
Take the 2nd left toward ...
我能把它缩短吗?太长了。
我可以这样做吗:
2nd right>2nd left>
有什么方法可以修改结果吗?我使用PHP开发web应用程序,并使用JSON格式显示API结果。编辑:API结果显示正确。但我想删除某些常用词,如"Take"、"The"、"at"等API结果显示我的代码的一部分:if ($data->status === 'OK') {
$route = $data->routes[0];
foreach ($route->legs as $leg) {
foreach ($leg->steps as $step) {
echo $step->html_instructions . "<br>'n";
这对我有效,希望对你也有效。。。
<?php
$test1 = 'Take the 2nd right.';
$test2 = 'Take the 2nd left toward the exit, then...';
$reg_find = '/Take the (.*?) (right|left).*/';
$reg_replace = '$1 $2';
$results = array(
preg_replace($reg_find, $reg_replace, $test1),
preg_replace($reg_find, $reg_replace, $test2)
);
echo implode('>', $results);
?>
编辑:
为了获得更灵活的解决方案,我创建了这个:
<?php
$test1 = 'Take the 2nd right.';
$test2 = 'Take the 2nd left toward the exit, then ...';
$remove = '/( ?)(take|then|at|toward|the|exit|'.|,)( ?)/i';
$results = array(
preg_replace($remove, '', $test1),
preg_replace($remove, '', $test2)
);
echo implode('>', $results);
?>