douyanzhou1450 2013-09-25 07:40
浏览 48
已采纳

如何从给定的字符串中提取data-url?

  $string = '<img alt="Peachtree St Apartment 2" data-url="http://i.oodleimg.com/item/3472578127t_1m_condos,_townhouses_&amp;_apts_for_sale_in_johnson_city_tn/?1377909664" src="http://i.oodleimg.com/a/animate/spinner-small.gif" class="lazyload-image">';

I want to extract the data-url parameter in PHP.

  • 写回答

2条回答 默认 最新

  • dougui2254 2013-09-25 07:52
    关注

    This is a great job for XPath! Don't know what XPath is? Let Wikipedia answer that:

    XPath, the XML Path Language, is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. XPath was defined by the World Wide Web Consortium (W3C).

    So for your specific user case, the query will be:

    //img[@class='lazyload-image']/@data-url
    

    So you can use it like:

    $query("//img[@class='lazyload-image']/@data-url");
    $xpath->query($query);
    

    Then you're free to iterate over it. I've taken the class appended to your img-tag into consideration in this matter, but feel free to remove that:

    //img/@data-url
    

    More info on how to use XPath with PHP (DOMXPath) in the manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失