【问题标题】:Can't get plain text google maps api html_instructions in php无法在 php 中获取纯文本谷歌地图 api html_instructions
【发布时间】:2018-08-27 23:27:18
【问题描述】:

我正在尽最大努力从谷歌地图方向 api 获取纯文本,它在 json 中显示 html_instructions。一切都用html编码,我想输出纯文本。

这就是我得到的 image 1

这就是我想要的:image 2

我尝试了任何类型的 preg_replace,但都帮不上忙。

谷歌地图 api 链接 Link

【问题讨论】:

  • 您能说明您是如何调用 API 的吗?你是怎么问路的?
  • 我添加了api的链接。
  • 如果您是一位经验丰富的 Javascript 程序员,您可以使用 DirectionsService 和 DirectionsRenderer 对象在地图上绘制路线或以英文文本(或任何语言)显示路线。 developers.google.com/maps/documentation/javascript/examples/…> 中的示例显示了如何以文本形式显示。获取文本的一种偷偷摸摸的方法是从 DirectionsRenderer 生成的 HTML 中读取它。这可能比尝试自己解析 JSON 更容易。
  • 谢谢,但我想你没有得到我的问题。你看过我上面贴的那些图片吗?
  • 是的,但我认为让 Google 对象为您解析会更容易。我相信我们可以使用 preg_replace() 完成您要求的操作,因此我会尽快测试并发布答案...

标签: php google-maps-api-3 map-directions


【解决方案1】:

编辑: 以前的代码 sn-p 被删除并替换为可行的小程序。

请注意,当您使用 json_decode() 处理数据时,像 \u003cb\u003eFlintergata\u003c/b\u003e 这样的 unicode 段将转换为 <b>Flintergata</b>。这有助于使 Regex 更具可读性。

注意$details数组是一个多级关联数组,所以你需要如图所示向下挖掘找到你需要的数据。

还要注意您提供的 URL 会导致 1 条路线,1 条腿。所以我提供的代码显示并处理了第一条路线的第一段。

如果您使用不同的 URL,您可能会获得多条路线,每条路线都包含多个步骤。该代码仍将处理第一条路线的第一条路线,但它的每条路线(带有外部循环)都会显示所有路线(下面未显示)。

正则字符串'"~>([A-Z].*?)

每一边的 '#' 是 PHP 分隔符 - 但您也可以使用其他字符,不会有任何区别。

<b></b> 表示每个匹配的字符串必须以 <b> 开头并以 </b> 结尾。

( ) 内部有一个“捕获组”,表示我们只想提取字符串的那一部分(不包括<b></b>)。

[A-Z] 表示以大写字母开头

.* 表示跟随 0 个或多个任意字符。

? 使 * non_greedy 因此在这种情况下,当它遇到下一个 < 时停止当前匹配。

每个字符串的匹配列表进入一个名为$matches 的数组,$matches[1] 是一个捕获组匹配数组(即删除<b></b> 中的文本)。

<?php

$json = file_get_contents("https://maps.googleapis.com/maps/api/directions/json?origin=sandnes&destination=vigrestad&key="); 
$details = json_decode($json,true);

// $details is a large associative array   
// print all the instructions for the first step of the fist leg of the first route
echo PHP_EOL."Here are the unfiltered html instructions for first leg of first route ".PHP_EOL.PHP_EOL;
$steps = $details['routes'][0]['legs'][0]['steps'];
foreach($steps as $step){
    echo($step['html_instructions']).PHP_EOL;  // print to see format
    // we see unicode html_entities have been replaced and now look like <b>  </b> etc
}

// now extra the required information from each step
echo PHP_EOL."Here are the filtered html instructions for first leg of first route ".PHP_EOL.PHP_EOL;
foreach ($steps as $step)
{ 
    //preg_match_all("~003e([A-Z].*?)\\\\u003c~", $step['html_instructions'], $match); // not needed now
    preg_match_all('#,<b>([A-Z].*?)</b>#, $step['html_instructions'], $match);  // now detects strings between '>' and '<'
    foreach($match[1] as $instructionPart)
    { 
        echo $instructionPart." "; 
    } 
    echo PHP_EOL; 

} 
?>

【讨论】:

  • 这更近了?
  • 很抱歉回复晚了。无论如何,谢谢你的工作。但我认为这对我有用。你上面写的代码。通过使用来自 json 的 file_get_contents 并使用你的代码没有给我任何影响。你能用完整的代码重写吗?
  • 我试过了 &lt;?php $json = file_get_contents("https://maps.googleapis.com/maps/api/directions/json?origin=sandnes&amp;destination=vigrestad&amp;key="); $details = json_decode($json,true); foreach ($details as $strn){ preg_match_all("~003e([A-Z].*?)\\\\u003c~", $strn, $match); foreach($match[1] as $name){ echo $name." "; } echo PHP_EOL; } ?&gt;
  • 好的,感谢您发送您尝试过的代码。见新回复。如果您需要更多帮助或解释,我建议您提出一个新问题并向我发送评论,以便我解决。我认为最好现在关闭它。
  • 非常感谢您帮助我。我想我通过将 json 更改为 xml 并从那里获取内容找到了更好的解决方案。你可以在这里看到我的代码:&lt;?php $url = htmlspecialchars_decode("https://maps.googleapis.com/maps/api/directions/xml?origin=sandnes&amp;destination= vigrestad&amp;key="); $content = file_get_contents($url); preg_match_all('#&amp;lt;b&amp;gt;(.*?)&amp;lt;/b&amp;gt;#m', $content, $matches_title); $title = $matches_title[1]; $title = str_replace(array("left", "right", "1st", "2nd", "3rd"), '', $title); echo implode("&lt;center&gt;&lt;br&gt;", $title); ?&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多