Retrieve ID PHP preg_match_all

时间:2015-06-30 19:20:21

标签: php preg-match preg-match-all

I want to retrieve "376901" from the following table:

<td class="evn_tdata_l">Reconciliation-Manager_RECON-MGMT_SFTP_T</td>
<td class="evn_tdata_l">376901</td>

Right now what I've tried is the following code:

    if (preg_match_all("/<td[^>]+?>Reconciliation-Manager_RECON-MGMT_SFTP_T<\/td><td[^>]+?>(.*)<\/td>/s", $html, $matches)) {
    print_r($matches);
} else {
    echo "No se encontró ninguna coincidencia.";

I dont see what seems to be wrong, could you please advise? Thank you.

2 个答案:

答案 0 :(得分:0)

\s*

之间添加<td>
/<td[^>]+?>Reconciliation-Manager_RECON-MGMT_SFTP_T<\/td>\s*<td[^>]+?>(.*)<\/td>/sU

答案 1 :(得分:0)

您的模式中的问题是,您没有在第一个结束标记</td>和后面的开始标记<td...之间描述换行符和最终的其他空白字符。

但是你可以使用一种不关心这些细节的方法,只使用html结构。 作为结构化数据,您可以使用xpath查询在XML或HTML内容中获取所需信息,例如:

$html = <<<'EOD'
<td class="evn_tdata_l">Reconciliation-Manager_RECON-MGMT_SFTP_T</td>
<td class="evn_tdata_l">376901</td>
EOD;

$dom = new DOMDocument;
$dom->loadHTML($html);

$xp = new DOMXPath($dom);

$query = '//td[@class="evn_tdata_l" and text()="Reconciliation-Manager_RECON-MGMT_SFTP_T"]/following-sibling::td[1]';

$result = $xp->query($query)->item(0)->nodeValue;

echo $result;