<table class="mainTable" cellpadding="0" cellspacing="0" align="center">
<tr>
<td class="day">test 1</td>
<td data-info="" class=" c"></td>
<td data-info="91" class="widgets c91">data 1</td>
<td data-info="" class=" c"></td>
<td data-info="109" class="widgets c109">data 2</td>
<td data-info="" class=" c"></td>
<td data-info="126" class="widgets c126">data 3</td>
</tr>
<tr>
<td class="day">test 2</td>
<td data-info="83" class="widgets c83">data 4<div class="triangle"></div></td>
<td data-info="" class=" c"></td>
<td data-info="100" class="widgets c100">data 5<div class="triangle"></div></td>
<td data-info="" class=" c"></td>
<td data-info="118" class="widgets c118">data 6<div class="triangle"></div></td>
<td data-info="" class=" c"></td>
</tr>
<tr>
<td class="day">test 3</td>
<td data-info="84" class="widgets c84">data 7</td>
<td data-info="92" class="widgets c92">data 8</td>
<td data-info="101" class="widgets c101">data 9</td>
<td data-info="110" class="widgets c110">data 10</td>
<td data-info="119" class="widgets c119">data 11</td>
<td data-info="127" class="widgets c127">data 12</td>
</tr>
</table>
我正在使用简单的html dom生成包含表中项目的数组。 我希望能够识别 td 元素是否具有div类'triangle'并将其输出到数组中。
答案 0 :(得分:1)
您可以在迭代的元素中调用find
方法。这是一个简单的例子。
foreach($html->find('td') as $element) {
if ($element->find('div')) {
echo "found div";
}
}
答案 1 :(得分:1)
在http://simplehtmldom.sourceforge.net/manual.htm处查看Simple DOM Parser的文档时,我发现找到包含带有“三角形”类的div的所有td的简单方法如下:
foreach($html->find('div.triangle') as $element){
echo $element->parent() . '<br>';
}
答案 2 :(得分:1)
这是一个可以尝试的工作示例:
<?php
$text = '<table class="mainTable" cellpadding="0" cellspacing="0" align="center">
<tr>
<td class="day">test 1</td>
<td data-info="" class=" c"></td>
<td data-info="91" class="widgets c91">data 1</td>
<td data-info="" class=" c"></td>
<td data-info="109" class="widgets c109">data 2</td>
<td data-info="" class=" c"></td>
<td data-info="126" class="widgets c126">data 3</td>
</tr>
<tr>
<td class="day">test 2</td>
<td data-info="83" class="widgets c83">data 4<div class="triangle"></div></td>
<td data-info="" class=" c"></td>
<td data-info="100" class="widgets c100">data 5<div class="triangle"></div></td>
<td data-info="" class=" c"></td>
<td data-info="118" class="widgets c118">data 6<div class="triangle"></div></td>
<td data-info="" class=" c"></td>
</tr>
<tr>
<td class="day">test 3</td>
<td data-info="84" class="widgets c84">data 7</td>
<td data-info="92" class="widgets c92">data 8</td>
<td data-info="101" class="widgets c101">data 9</td>
<td data-info="110" class="widgets c110">data 10</td>
<td data-info="119" class="widgets c119">data 11</td>
<td data-info="127" class="widgets c127">data 12</td>
</tr>
</table>';
echo "<div>Original Text: <xmp>$text</xmp></div>";
$html = str_get_html($text);
$divArray = $html->find('div');
// if find exists
if ($divArray) {
echo '<br>Find function found '. count($divArray) . ' results: ';
foreach($divArray as $key=>$div){
echo '<br>'.$key . ': ' . $div->tag . ' with class = ' . $div->class;
}
}
else
echo "Find() fails !";
?>
由于您的输入html仅包含类triangle
的div标记,因此您可以仅搜索div
或使用类triangle
搜索div ...
但是如果你有更多div标签与其他类或没有任何类,那么你绝对要搜索div.triangle
以获得正确的结果。
有关详细信息,请参阅: