dongquanjie9328 2012-05-07 15:13
浏览 42
已采纳

想要从Google Maps嵌入式iframe中提取街道地址

Okay, here is what I have going on.

Some background: I'm making changes to an existing WordPress website that someone else built. They created a textarea where the client copies and pastes an iframe embed for a Google Map. This is for properties that the client posts on their website.

The dilemma: This is all well and good but I'm rebuilding the detail page for their properties and I want to strip out all of the iframe information and only leave the property address so that I can use it to create a new map via a Google Maps V3 jQuery plugin.

I want to turn this:

<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=5475+NW+75+AVE,+Ocala+FL+34482&aq=&sll=29.300577,-82.294762&sspn=0.006755,0.013304&vpsrc=0&ie=UTF8&hq=&hnear=5475+NW+75th+Ave,+Ocala,+Florida+34482&t=m&z=14&ll=29.244022,-82.241361&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=5475+NW+75+AVE,+Ocala+FL+34482&aq=&sll=29.300577,-82.294762&sspn=0.006755,0.013304&vpsrc=0&ie=UTF8&hq=&hnear=5475+NW+75th+Ave,+Ocala,+Florida+34482&t=m&z=14&ll=29.244022,-82.241361" style="color:#0000FF;text-align:left">View Larger Map</a></small>

Into this:

5475 NW 75 AVE, Ocala FL 34482

I think I'm on the right track by looking into preg_replace() but the regular expression for this is what gets me. Alternatively, if I can extract the coordinates that would help too. I can use either the address or the lat and long coordinates to get the job done.

Thanks in advance for any assistance I can get!

Edit with solution since my SO rep is still low:

Thanks to Mario I was able to get the job done. Here is my finalized code for anyone that it may help.

You started with this:

<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=5475+NW+75+AVE,+Ocala+FL+34482&aq=&sll=29.300577,-82.294762&sspn=0.006755,0.013304&vpsrc=0&ie=UTF8&hq=&hnear=5475+NW+75th+Ave,+Ocala,+Florida+34482&t=m&z=14&ll=29.244022,-82.241361&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=5475+NW+75+AVE,+Ocala+FL+34482&aq=&sll=29.300577,-82.294762&sspn=0.006755,0.013304&vpsrc=0&ie=UTF8&hq=&hnear=5475+NW+75th+Ave,+Ocala,+Florida+34482&t=m&z=14&ll=29.244022,-82.241361" style="color:#0000FF;text-align:left">View Larger Map</a></small>

And you want this:

5475 NW 75 AVE, Ocala FL 34482

This is what worked for me:

// We first find and extract the 'src' from the iframe
// $map is my original iframe embed
// $q is our extracted and stripped text
preg_match('#q=([^&"]+)#', $map, $match)
and ($q = urldecode($match[1]));

// Now you can echo out the address or use it elsewhere.
// In my case, I am using jQuery goMap (http://www.pittss.lv/jquery/gomap)
// and can add a new point on the map via $q
echo $q;
  • 写回答

1条回答 默认 最新

  • doulu3808 2012-05-07 15:56
    关注

    If you insist on the overkill solution, you would first use a HTML traversal library like QueryPath to split up the HTML and get the attribute:

    $url = qp($html)->find("iframe")->attr("src");
    

    But that's pointless and in practice you should just extract the URL from your text snippet:

    preg_match('#http://[^"\']+#', $html, $match)
    and ($url = $match[0]);
    

    From there just split it up with parse_url($url, PHP_URL_QUERY) and extract the bits with parse_str($qs, $vars) so you get $var["q"].

    But if it's a somewhat coherent input you could just carve out the q= parameter with:

    preg_match('#q=([^&"]+)#', $html, $match)
    and ($q = urldecode($match[1]));
    

    Even more lazy would be just using parse_str on the whole HTML snippet, and praying the leading and trailing garbage does not interfere with the desired snippet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog