PHP& RegEx在源代码中查找多个链接

时间:2015-09-11 21:30:31

标签: php regex string parsing

(希望标题有意义,如果有更好的方式来描述随意更新)

总结:我有一堆HTML源代码,我负责完成并从中挑选出特定的链接。

在代码中..识别我需要的链接。所有这些都始于:

<a href="javascript:void(0)"

并以:

结束
</em>

所以我想抓住源代码中的所有链接,这些链接以上面的例子为开头。

我想要从源代码中获取1或70个链接。

全部以

开头
<a href="javascript:void(0)"

我希望抓住所有内容(包括)下一个/立即:

</em>

我有一些其他正则表达式,我用来重新格式化东西后我从源代码中获取链接(字符串)..但我正在寻找(希望)有一种更自动化的方式来解析这些链接超出源代码。

我把PHP,因为它可用作中间步骤..或者我可以使用Notepad ++(就像我通常那样)

我有什么选择,在没有其他源代码的情况下,可以使用什么正则表达式从页面中获取多个链接?

更新

以下是我尝试在源代码中获取的链接类型的示例(可以是1-2到70之间的任何地方)

<a href="javascript:void(0)" onclick="window.open('/some/url/presentations/index.php?filename=1105 name-v2.pdf'); return false;"><strong>Presentation Title</strong></a>&nbsp;&nbsp;<img alt="PDF" border="0" height="12" src="/images/template/icn_pdf.gif" width="12" /><br />
Presenter Name XYZ - <em>Institution Name XYZ</em>
  • </em><a href开始...
  • 之后/之外

标签以完整内容显示:

<table>
<tr>
    <td>junk</td>
    <td><a href="javascript:void(0)" onclick="window.open('/some/url/presentations/index.php?filename=1105 name-v2.pdf'); return false;"><strong>Presentation Title</strong></a>&nbsp;&nbsp;<img alt="PDF" border="0" height="12" src="/images/template/icn_pdf.gif" width="12" /><br />
Presenter Name XYZ - <em>Institution Name XYZ</em></td>
</tr>

<tr>
    <td>junk</td>
    <td><a href="javascript:void(0)" onclick="window.open('/some/url/presentations/index.php?filename=1105 name-v2.pdf'); return false;"><strong>Presentation Title</strong></a>&nbsp;&nbsp;<img alt="PDF" border="0" height="12" src="/images/template/icn_pdf.gif" width="12" /><br />
Presenter Name XYZ - <em>Institution Name XYZ</em></td>
</tr>

<tr>
    <td>junk</td>
    <td><a href="javascript:void(0)" onclick="window.open('/some/url/presentations/index.php?filename=1105 name-v2.pdf'); return false;"><strong>Presentation Title</strong></a>&nbsp;&nbsp;<img alt="PDF" border="0" height="12" src="/images/template/icn_pdf.gif" width="12" /><br />
Presenter Name XYZ - <em>Institution Name XYZ</em></td>
</tr>

<tr>
    <td>junk</td>
    <td><a href="javascript:void(0)" onclick="window.open('/some/url/presentations/index.php?filename=1105 name-v2.pdf'); return false;"><strong>Presentation Title</strong></a>&nbsp;&nbsp;<img alt="PDF" border="0" height="12" src="/images/template/icn_pdf.gif" width="12" /><br />
Presenter Name XYZ - <em>Institution Name XYZ</em></td>
</tr>
</table>
冲洗&amp; amp;重复..

显然链接不同......等等。

2 个答案:

答案 0 :(得分:0)

这应该是你要找的东西:

//define html as a string here
var Regex = new RegExp('<a +href="javascript:void\(0\)"[^]*?<\/em>', 'g');
var Matches = [];
while ((result = Regex.exec(html)) !== null) {
    Matches.push(result[0]);
}

答案 1 :(得分:0)

这是一个非正则表达式解决方案: 类:

<?php

class Parser {
    private $_position, $_html;

    function __construct($html) {
        $this->_html = $html;
    }

    private function SkipTo($text) {
        $p = strpos($this->_html, $text, $this->_position);
        if ($p !== false)
            $this->_position = $p + strlen($text);
        else
            return false;
        return true;

    }

    private function ReadTo($text) {
        $p = strpos($this->_html, $text, $this->_position);
        $result = false;

        if ($p !== false) {
            $result = substr($this->_html, $this->_position, $p - $this->_position);
            $this->_position = $p;
        }

        return $result;
    }

    public function Get($from, $to) {
        $result = array();
        while($this->SkipTo($from)) {
            $result[] = $this->ReadTo($to);
        }
        return $result;
    } 
}

像这样使用:

require_once 'parser.class.php';

$html = "<a href=\"javascript:void(0)\" onclick=\"window.open('/some/url/presentations/index.php?filename=1105 name-v2.pdf'); return false;\"><strong>Presentation Title</strong></a>&nbsp;&nbsp;<img alt=\"PDF\" border=\"0\" height=\"12\" src=\"/images/template/icn_pdf.gif\" width=\"12\" /><br />
Presenter Name XYZ - <em>Institution Name XYZ</em>";

$parser = new Parser($html);
$result = $parser->Get('<a href="javascript:void(0)"', '</em>');

foreach($result as $res) {
    echo $res . "<br>";
}