首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google directions API: JSON

Google directions API: JSON
EN

Stack Overflow用户
提问于 2012-08-06 12:49:03
回答 1查看 526关注 0票数 1

我在我的web应用程序中使用了Google directions API,有没有办法缩短Google提供的方向描述?

我是说,举个例子

代码语言:javascript
复制
Take the 2nd right.
Take the 2nd left toward ...

我能把它剪短吗?太长了。

我可以这样做吗:

代码语言:javascript
复制
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";

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-06 13:07:54

这对我很管用,希望对你也管用。

代码语言:javascript
复制
<?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);
?>

编辑:

为了获得更灵活的解决方案,我创建了以下内容:

代码语言:javascript
复制
<?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);
?>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11822688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档