我在我的web应用程序中使用了Google directions API,有没有办法缩短Google提供的方向描述?
我是说,举个例子
Take the 2nd right.
Take the 2nd left toward ...我能把它剪短吗?太长了。
我可以这样做吗:
2nd right>2nd left>有什么方法可以修改结果吗?我使用PHP开发web应用程序,使用JSON格式显示API结果。编辑:接口结果显示正确。但是我想删除某些常见的单词,比如'Take','The','at‘等显示部分代码的API result:if ($data->status === 'OK') { $route = $data->routes[0]; foreach ($route->legs as $leg) { foreach ($leg->steps as $step) { echo $step->html_instructions . "<br>\n";
发布于 2012-08-06 13:07:54
这对我很管用,希望对你也管用。
<?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);
?>https://stackoverflow.com/questions/11822688
复制相似问题