在PHP中的html文档中获取两个其他字符串之间的所有字符串

时间:2017-03-28 22:53:37

标签: php html web-crawler

我目前正在创建某种抓取工具/代理服务器。它可以浏览网站,并在浏览时仍保留在我的网站上。但我在加载网站时考虑过,同时获取所有链接和数据。

因此该网站包含许多"< tr>"(没有空格)再次包含很多其他东西。

以下是网站上许多例子:

<tr>
    <td class="vertTh">
        <center>
            <a href="/s/browse/other.php">Other</a>
            <br>
            <a href="/s/browse/documents.php">Document</a>
        </center>
    </td>
    <td>
        <div class="Name">
            <a href="/s/database/Document_Title_Info" class="Link">Document Title Info</a>
        </div>
        <a href="http://example.com/source/to/document/which%20can%20be%20very%20long%20and%20have%20weird%20characters" title="Source">
            <img src="/static/img/icon-source.png" alt="Source">
        </a>
        <font class="Desc">Uploaded 03-24&nbsp;14:02, Size 267.35&nbsp;KB, ULed by <a class="Desc" href="/s/user/username/" title="Browse username">username</a></font>
    </td>
    <td align="right">67</td>
    <td align="right">9</td>
</tr>

用户浏览代理网站,当他们这样做时,它会从原始网站上获取信息。 我想出了如何在两个单词之间得到一个字符串,但我不知道如何将它变成一个&#34; foreach&#34;代码或其他东西。

所以,让我们说我想得到源链接。然后我会做这样的事情:

$url = $_GET['url'];
$str = file_get_contents('https://database.com/' . $url);

$source = 'http://example.com/source/to/' . getStringBetween($str,'example.com/source/to/','" title="Source">'); // Output looking like this: http://example.com/source/to/document/which%20can%20be%20very%20long%20and%20have%20weird%20characters

function getStringBetween($str,$from,$to)
{
    $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
    return substr($sub,0,strpos($sub,$to));
}

但我不能这样做,因为这些字符串有多个。所以我想知道是否有任何一种方法可以获得所有这些字符串的来源,名称和大小?

1 个答案:

答案 0 :(得分:-1)

您可能希望使用preg_match_all,以便获得许多匹配项的列表。然后你可以循环它。

http://php.net/manual/en/function.preg-match-all.php

$html = '<tr>
    <td class="vertTh">
        <center>
            <a href="/s/browse/other.php">Other</a>
            <br>
            <a href="/s/browse/documents.php">Document</a>
        </center>
    </td>
    <td>
        <div class="Name">
            <a href="/s/database/Document_Title_Info" class="Link">Document Title Info</a>
        </div>
        <a href="http://another-example.com/source/to/document/which%20can%20be%20very%20long%20and%20have%20weird%20characters" title="Source">
            <img src="/static/img/icon-source.png" alt="Source">
        </a>
        <a href="http://example.com/source/to/document/which%20can%20be%20very%20long%20and%20have%20weird%20characters" title="Source">
            <img src="/static/img/icon-source.png" alt="Source">
        </a>
        <font class="Desc">Uploaded 03-24&nbsp;14:02, Size 267.35&nbsp;KB, ULed by <a class="Desc" href="/s/user/username/" title="Browse username">username</a></font>
    </td>
    <td align="right">67</td>
    <td align="right">9</td>
</tr>';

// use | as delimiter for pattern to make it a little cleaner
preg_match_all('|href="(http://.+?)" title="Source"|', $html, $matches);
// loop over $matches
var_dump($matches);

foreach ($matches[1] as $match) {
    // $match == http://example.com/source/to/document/which%20can%20be%20very%20long%20and%20have%20weird%20characters
}

您可以在... http://phpfiddle.org/尝试此示例,也可以在本地.php文件中运行。祝你好运。

仅供参考:我添加了一个额外的锚标记来说明找到另一个来源。

相关问题