【问题标题】:php preg_match_all words starting with?php preg_match_all 以什么开头的单词?
【发布时间】:2014-03-25 04:04:06
【问题描述】:

我正在使用file_get_contents 从外部站点提取日历,因此我可以在其上使用 jQuery .load。

为了解决这种方法的相对路径问题,我正在使用 preg_match_all。

原来如此

preg_match_all("/<a href='([^\"]*)'/iU", $string, $match);

获取所有出现的<a href = '' 我所追求的只是单引号内的链接。 现在每个链接都以“?date”开头,所以我有<a href='?date=4%2F9%2F2014&a' 等。

如何有效地获取所有 <a href= 事件中单引号之间的字符串。

【问题讨论】:

    标签: php preg-match-all


    【解决方案1】:

    使用Dom parser<a> 标签中获取href

    <?php
    $file =  "your.html";
    $doc = new DOMDocument();
    $doc->loadHTMLFile($file);
    $elements = $doc->getElementsByTagName('a');
    foreach ($elements as $tag) {
        echo $tag->getAttribute('href');
    }
    

    【讨论】:

    • 这是另一种方式,比我开始的方式要好得多。谢谢!
    猜你喜欢
    • 2013-09-30
    • 2014-06-11
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    相关资源
    最近更新 更多