我一直在使用简单的HTML DOM,并一直热爱我能用它做什么。但是我遇到了一些我想做的事情。首先给出html的基本概念
<html>
<div><div><table><tr><td class="alt2"> texty texty </td></tr></table></div></div>
Text text
<div><div><table><tr><td class="alt2"> texty2 texty2 </td></tr></table></div></div>
Text2 text2
<div><div><table><tr><td class="alt2"> texty3 texty3 </td></tr></table></div></div>
Text2 text3
</html>
所以基本上我需要在TD的大胆中采取什么,然后在这个div的每个下面放置什么。
所以我知道我可以做$ text = $ html-&gt; find('td [class = alt2]')例如获取“texty texty”但是如何将“text text”与之后,
如果我抓住texty2 texty2,如何将text2 text 2与之关联?
最后,我瞄准的输出是<p>texty texty</p><br>text text
类的东西
我应该指出div可以有不同的类等等,所以简单的reg表达式不起作用(如果可能的话,我想坚持使用简单的html dom)
-
更新:
我已经制作了这么一点代码
if ($html) {
// Pulls the div that holds the links to the champion profiles (where they have the english names)
$text = $html->find('td[class="alt2"]');
$count = 0;
foreach ($text as $holder) {
$tdholder[] = $holder->innertext;
$html->find('div',$count*2)->innertext = $holder->innertext;
echo $holder."<br><br>";
$count++;
}
}
此代码的唯一问题是它是否以div的总数计算。这在我的例子中是可以的,因为总有2.但如果有2或3,这就成了一个问题。所以现在我只需要弄清楚如何抓住第一个,第二个或第三个顶级div,我应该很好。
答案 0 :(得分:0)
Text text
,Text2 text2
和Text2 text3
不在任何使您的请求含糊不清的html标记中
下面的解决方案可以正常工作but it is code specific
它只适用于上面的HTML示例添加原始HTML以便更好地实现
$list = array();
foreach ( $html->find('text') as $td ) {
$list[] = $td->plaintext;
}
echo "<pre>";
foreach (array_chunk(array_filter(array_map("trim", $list)), 2) as $info ) {
vprintf("TD: %s,\tTXT: %s \n", $info);
}
输出
TD: texty texty, TXT: Text text
TD: texty2 texty2, TXT: Text2 text2
TD: texty3 texty3, TXT: Text2 text3