使用正则表达式从谷歌地图嵌入链接中提取src

时间:2013-03-11 16:12:19

标签: php regex google-maps

各位大家好,感谢您的阅读。 我有这样一个链接:

<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=monte+rosa&amp;aq=&amp;sll=45.454082,9.213138&amp;sspn=0.009016,0.01929&amp;t=h&amp;gl=it&amp;ie=UTF8&amp;hq=&amp;hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&amp;ll=45.690627,8.824349&amp;spn=0.008978,0.01929&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe><br /><small><a href="https://maps.google.it/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=monte+rosa&amp;aq=&amp;sll=45.454082,9.213138&amp;sspn=0.009016,0.01929&amp;t=h&amp;gl=it&amp;ie=UTF8&amp;hq=&amp;hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&amp;ll=45.690627,8.824349&amp;spn=0.008978,0.01929&amp;z=14&amp;iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>

我想只提取这个

https://maps.google.it/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=monte+rosa&amp;aq=&amp;sll=45.454082,9.213138&amp;sspn=0.009016,0.01929&amp;t=h&amp;gl=it&amp;ie=UTF8&amp;hq=&amp;hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&amp;ll=45.690627,8.824349&amp;spn=0.008978,0.01929&amp;z=14&amp;iwloc=A&amp;output=embed

换句话说,我想在src =“之前提取所有内容,直到链接结束”。 我一直在尝试使用正则表达式,但我无法弄清楚正确的语法。一些帮助将非常感激。

1 个答案:

答案 0 :(得分:3)

$html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=monte+rosa&amp;aq=&amp;sll=45.454082,9.213138&amp;sspn=0.009016,0.01929&amp;t=h&amp;gl=it&amp;ie=UTF8&amp;hq=&amp;hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&amp;ll=45.690627,8.824349&amp;spn=0.008978,0.01929&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe><br /><small><a href="https://maps.google.it/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=monte+rosa&amp;aq=&amp;sll=45.454082,9.213138&amp;sspn=0.009016,0.01929&amp;t=h&amp;gl=it&amp;ie=UTF8&amp;hq=&amp;hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&amp;ll=45.690627,8.824349&amp;spn=0.008978,0.01929&amp;z=14&amp;iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>';
preg_match('~iframe.*src="([^"]*)"~', $html, $result);
var_dump($result[1]);