preg_match()查找表中的所有值? [英] preg_match() find all values inside of table?
本文介绍了preg_match()查找表中的所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hey guys,
a curl函数返回一个字符串$ widget,其中包含正则html - >两个div,其中第一个div包含< td>
'。
hey guys,
a curl function returns a string $widget that contains regular html -> two divs where the first div holds a table with various values inside of <td>
's.
我不知道什么是最简单和最好的方法,我只提取< td>
因此我有空白值没有剩余的html。
i wonder what's the easiest and best way for me to extract only all the values inside of the <td>
's so i have blank values without the remaining html.
任何想法,preg_match的模式应该是什么样子?
any idea what the pattern for the preg_match should look like?
谢谢。 b $ b
推荐答案
您将使用DOM解析器来完成该任务:
You're betting off using a DOM parser for that task:
$html = <<<HTML
<div>
<table>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
<tr>
<td>hello</td>
<td>world</td>
</tr>
</table>
</div>
<div>
Something irrelevant
</div>
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$tds = $xpath->query('//div/table/tr/td');
foreach ($tds as $cell) {
echo "{$cell->textContent}\n";
}
将输出:
foo
bar
hello
world
这篇关于preg_match()查找表中的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文