使用来自 table 的简单 html dom 解析内容,但有一个条件
Parsing content with Simple html dom from a table but with a condition
所以我只想在 'echo' 中显示当天情况为 'Yes' 的人,所以我们假设今天只有 'John' 来自我的数组列表情况 'Yes'.
$people = array('John', 'Greg', 'Mike', 'James', 'Jason');
$situation = array('Yes');
$html = str_get_html('<table class="mytab">
<tr>
<th>Name</th>
<th>Situation</th>
</tr>
<tr>
<td>
<a href="#">John</a>
</td>
<td class="s">
<strong>Yes</strong>
</td>
</tr>
<tr>
<td>
<a href="#">Allan</a>
</td>
<td class="s">
<strong>No</strong>
</td>
</tr>
<tr>
<td>
<a href="#">James</a>
</td>
<td class="s">
<strong>No</strong>
</td>
</tr>
</table>');
$table = $html->find('table', 0);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$content= array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$content[] = $cell->plaintext;
}
$rowData[] = $content;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
上面的代码将显示:
John Yes
Allan No
James No
这让我发疯,我想不通!!
我怎样才能在 <td>s
中搜索并只找到当天 'Yes' 的人?
假设您的 $html 是一个有效的 XML 字符串(意味着每个标签都正确关闭,并且有一个父标签,标签外没有文本),请执行以下操作:
$html="<table>..";
$xml = new SimpleXmlElement($html);
$result=array();
foreach($xml->tr as $tr){
//you have your td, see what it has
$name='';
foreach($tr->td as $td) {
if(isset($td->a))
$name=$td->a;
if(isset($td->strong) && $td->strong=='Yes'){
$result[]=strval($name);
}
}
}
print_r($result); //this will have array with name of those where YES
Array
(
[0] => John
)
EDIT: ,如果您有 html 的众所周知的 XML 格式,则无需去检查每个 td。在这里,我删除了嵌套循环,并将复杂度从 O(n2) 降低到 O(n)。
$result=array();
foreach($xml->tr as $tr){
//just in case see if a tag is there
if(isset($tr->td[0]->a))
$name = $tr->td[0]->a;
$td2 = $tr->td[1];
if(isset($td2->strong) && $td2->strong=='Yes'){
$result[]=strval($name);
}
}
print_r($result);
所以我只想在 'echo' 中显示当天情况为 'Yes' 的人,所以我们假设今天只有 'John' 来自我的数组列表情况 'Yes'.
$people = array('John', 'Greg', 'Mike', 'James', 'Jason');
$situation = array('Yes');
$html = str_get_html('<table class="mytab">
<tr>
<th>Name</th>
<th>Situation</th>
</tr>
<tr>
<td>
<a href="#">John</a>
</td>
<td class="s">
<strong>Yes</strong>
</td>
</tr>
<tr>
<td>
<a href="#">Allan</a>
</td>
<td class="s">
<strong>No</strong>
</td>
</tr>
<tr>
<td>
<a href="#">James</a>
</td>
<td class="s">
<strong>No</strong>
</td>
</tr>
</table>');
$table = $html->find('table', 0);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$content= array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$content[] = $cell->plaintext;
}
$rowData[] = $content;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
上面的代码将显示:
John Yes
Allan No
James No
这让我发疯,我想不通!!
我怎样才能在 <td>s
中搜索并只找到当天 'Yes' 的人?
假设您的 $html 是一个有效的 XML 字符串(意味着每个标签都正确关闭,并且有一个父标签,标签外没有文本),请执行以下操作:
$html="<table>..";
$xml = new SimpleXmlElement($html);
$result=array();
foreach($xml->tr as $tr){
//you have your td, see what it has
$name='';
foreach($tr->td as $td) {
if(isset($td->a))
$name=$td->a;
if(isset($td->strong) && $td->strong=='Yes'){
$result[]=strval($name);
}
}
}
print_r($result); //this will have array with name of those where YES
Array
(
[0] => John
)
EDIT: ,如果您有 html 的众所周知的 XML 格式,则无需去检查每个 td。在这里,我删除了嵌套循环,并将复杂度从 O(n2) 降低到 O(n)。
$result=array();
foreach($xml->tr as $tr){
//just in case see if a tag is there
if(isset($tr->td[0]->a))
$name = $tr->td[0]->a;
$td2 = $tr->td[1];
if(isset($td2->strong) && $td2->strong=='Yes'){
$result[]=strval($name);
}
}
print_r($result);