【问题标题】:PHP preg_match return the string between two stringsPHP preg_match 返回两个字符串之间的字符串
【发布时间】:2015-10-27 17:00:17
【问题描述】:

如何从代码中获取此链接“http://example.com/view.php?id=5841”:

<h3 class="coursename"><a class="" href="http://example.com/view.php?id=521">D<span class="highlight">LAW</span> <span class="highlight">130</span>Management</a></h3><div class="moreinfo"></div></div><div class="content"><ul class="teachers"><li>Teacher: <a href="http://example.com/">John</a></li></ul><div class="coursecat">Category: <a class="" href="http://example.com/">First</a></div></div></div><div class="coursebox clearfix even" data-courseid="5841" data-type="1"><div class="info"><h3 class="coursename"><a class="" href="http://example.com/view.php?id=5841"><span class="highlight">LAW</span> <span class="highlight">130`

我试过了:

preg_match('/href="(.*)"><span class="highlight">LAW/isU',$BBB,$AAA);

结果是:

http://example.com/view.php?id=521">D<span class="highlight">LAW</span> <span class="highlight">130</span>Management</a></h3><div class="moreinfo"></div></div><div class="content"><ul class="teachers"><li>Teacher: <a href="http://example.com/">John</a></li></ul><div class="coursecat">Category: <a class="" href="http://example.com/">First</a></div></div></div><div class="coursebox clearfix even" data-courseid="5841" data-type="1"><div class="info"><h3 class="coursename"><a class="" href="http://example.com/view.php?id=5841

【问题讨论】:

  • 你想要所有的链接,还是带有 id 或带有特定 id 的链接,如果你想要所有这些就足够了 preg_match('href="(.*?)"', $data, $res); `

标签: php html-parsing preg-match


【解决方案1】:

改用这个:

/href="(.[^<]*?)"><span class="highlight">LAW/isU

这是一种告诉 Regex 找到与您想要的匹配的最短表达式的简单方法。

【讨论】:

    【解决方案2】:

    使用 XPath 查询:

    libxml_use_internal_errors(true);
    $dom = new DOMDocument;
    $dom->loadHTML($yourHTML);
    
    $xp = new DOMXPath($dom);
    $link = $xp->query('//a[span[@class="highlight"]][starts-with(.,"LAW")][1]/@href')->item(0)->nodeValue;
    
    echo $link;
    

    查询详情:

    // # axe: anywhere in the DOM tree
    a  # axe: a "a" tag
    [span[@class="highlight"]] # predicate: the "a" tag has for direct child a "span" tag 
                               # with a "class" attribute equal to "highlight"  
    [starts-with(.,"LAW")]     # predicate: its text content begins with "LAW"
    [1]                        # predicate: first occurrence (no need to search another one)
    /@href                     # axe: its "href" attribute 
    

    【讨论】:

    • 我是初级程序员,我不知道如何使用 XPath 查询 >> 无论如何非常感谢您
    • @Akram:DOM 是 HTML 或 XML 文档的树形表示。 XPath 是一种设计用于查询这棵树的语言。使用您的数据的工作示例:eval.in/457993
    猜你喜欢
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 2019-08-18
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多