【问题标题】:regexp achor href url that contains street address match包含街道地址匹配的正则表达式锚点 href url
【发布时间】:2013-04-21 20:14:25
【问题描述】:

我喜欢从 html 字符串中获取锚标记定义中的 url。 html 的结构相当好,但我试图收集的字符串包含谷歌地图的地址,并且可能非常不同。我正在尝试使用 preg_match_all 获取所有匹配的 url。

<tr><td><a href="http://maps.google.com/maps?q=4165 E LIVE OAK AVE,">map</a></td></tr>
<tr><td><a href="http://maps.google.com/maps?q=8000 SUNSET BLVD, LOS ANGELES,">map</a></td></tr>
<tr><td><a href="http://maps.google.com/maps?q=30600 THOUSAND OAKS BLVD, AGOURA,">map</a></td></tr>
<tr><td><a href="http://maps.google.com/maps?q=9090 19TH ST, ALTA LOMA,">map</a></td></tr>
<tr><td><a href="http://maps.google.com/maps?q=185 W ALTADENA DR, ALTADENA,">map</a></td></tr>
<tr><td><a href="http://maps.google.com/maps?q=620 E MOUNT CURVE AVE,">map</a></td></tr>

【问题讨论】:

    标签: php regex url preg-match-all street-address


    【解决方案1】:

    试试下面的正则表达式:

    /http:\/\/maps.google.com\/maps\?q[^"]+(?=")/
    

    但页面可能包含您提供的 HTML 结构之外的类似 URL,那么最好使用更复杂的正则表达式:

    /(?<=<tr><td><a href=")http:\/\/maps.google.com\/maps\?q[^"]+(?=">map<\/a><\/td><\/tr>)/
    

    【讨论】:

    • 谢谢你给我的东西是我需要缩短我之前尝试使用的东西来获取所有网址。这是我现在所拥有的:
      code /\b(?:(?:https?|ftp):\/\/|www\.)[^"]+(?=")/ code
    猜你喜欢
    • 2012-03-12
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多