PHP 简单 HTML DOM 得到两个不同的单元格 类 (内联)但一个可能是空的
PHP Simple HTML DOM getting two different cell classes (inline) but one may be empty
好吧,我到处搜索并尝试了很多东西(3 小时),但是,我仍然很困惑。
这是我的 HTML 来自 somepage.com 的片段。
<table class="infotable" cellpadding="0" cellspacing="0" width="185">
<tr>
<td class="what"">First name:</td>
<td class="whatdet">Jim</td>
</tr>
<tr>
<td class="what">Last name:</td>
<td class="whatdet">Bo</td>
</tr>
<tr>
<td class="what">Age:</td>
<td class="whatdet"></td> <!--PROBLEM IS HERE WITH EMPTY CELL-->
</tr>
<tr>
<td class="what">Sex:</td>
<td class="whatdet">Rarely</td>
</tr>
<tr>
<td class="what">City:</td>
<td class="whatdet"></td> <!--PROBLEM IS HERE WITH EMPTY CELL-->
</tr>
<tr>
<td class="what">State:</td>
<td class="whatdet">California</td>
</tr>
</table>
这是我的代码片段,其中包含一个测试,试图通过 isset 行向我显示更多信息。 (是的,我显然是一无所知)
require_once 'simpledom/simple_html_dom.php';
$html = file_get_html('http://somepage.com/');
$i=0;
$tabletitles = array(); /* Get the titles 'what' Cell Names */
$tabledetails = array(); /* Get the Details in 'whatdet' Cells */
$tables = $html->find('table[@class="infotable"]'); /* Where both reside in */
foreach($tables as $table) {
$titles = $table->find('td[@class="what"]');
$titlesd = $table->find('td[@class="whatdet"]');
foreach($titles as $title) {
/*UPDATE NOTICED A PROBLEM WITH a character like $ so I added */
/*will do the same in $titlesd if I can figure out how to get it */
$title1 = preg_replace('/([?#^&*()$\/])/', '\\', $title);
echo $title1; /*Changed from $title*/
if (isset($titlesd[$i])) /*this is just for testing*/
echo $titlesd[$i].' is either 0, empty, or not set at all';
/* WHAT I WANT is echo '<tr><td>'. $title .'</td><td>'. $titlesd[$i] . </td></tr>;*/
$i++;
}
}
我要尝试的是:
------------|----------
First name | Jim
----------- |---------
Last name | Bo
----------- |---------
Age |
----------- |---------
Sex | Rarely
----------- |---------
City |
----------- |---------
State | California
----------- |---------
但是我现在得到的是:
------------|----------
First name | Jim
----------- |---------
Last name | Bo
----------- |---------
Age | Rarely
----------- |---------
Sex | California
----------- |---------
City |
----------- |---------
State |
----------- |---------
我似乎无法弄清楚如何将 "blank" 分配给 $titlesd[$i]
或在循环中跳过它。所以,我不断得到不想要的结果。 (至少可以说)
所以,我再次恳求一位大师再给我上一堂宝贵的课。
谢谢..
使用
$titlesd = ($table->find('td[@class="whatdet"]')) ? $table->find('td[@class="whatdet"]') : "";
如果我没记错的话,这就是你想要做的:
require_once 'simpledom/simple_html_dom.php';
$html = file_get_html('http://somepage.com/');
foreach($html->find('table[@class="infotable"]') as $table) {
foreach($table->find('tr') as $line) {
$titles = $line->find('td[@class="what"]', 0);
$titlesd = $line->find('td[@class="whatdet"]', 0);
echo '<tr>'
.'<td>'.htmlspecialchars($titles).'</td>'
.'<td>'.htmlspecialchars($titlesd).'</td>'
.'</tr>';
}
}
我解释一下:
- 首先,我选择
<tr>
行,因为这是您要打印的输出内容
- 接下来,我尝试为当前
<tr>
行 上的标题和数据获取第一个匹配项 (index 0
as 2nd parameter with ->find()
)
- 最后,我用
htmlspecialchars()
打印它们以避免 &>'<"
出现问题
nb:因为 ->find()
与索引将 return NULL
如果没有找到,我让这段代码假设最坏情况下 echo $titlesd
会是 NULL
并且不显示任何内容
Blag 的回答很好,但可以更简单:
foreach($html->find('.infotable tr') as $tr){
echo $tr->find('td.what',0)->text();
echo $tr->find('td.whatdet',0)->text();
echo "\n";
}
好吧,我到处搜索并尝试了很多东西(3 小时),但是,我仍然很困惑。
这是我的 HTML 来自 somepage.com 的片段。
<table class="infotable" cellpadding="0" cellspacing="0" width="185">
<tr>
<td class="what"">First name:</td>
<td class="whatdet">Jim</td>
</tr>
<tr>
<td class="what">Last name:</td>
<td class="whatdet">Bo</td>
</tr>
<tr>
<td class="what">Age:</td>
<td class="whatdet"></td> <!--PROBLEM IS HERE WITH EMPTY CELL-->
</tr>
<tr>
<td class="what">Sex:</td>
<td class="whatdet">Rarely</td>
</tr>
<tr>
<td class="what">City:</td>
<td class="whatdet"></td> <!--PROBLEM IS HERE WITH EMPTY CELL-->
</tr>
<tr>
<td class="what">State:</td>
<td class="whatdet">California</td>
</tr>
</table>
这是我的代码片段,其中包含一个测试,试图通过 isset 行向我显示更多信息。 (是的,我显然是一无所知)
require_once 'simpledom/simple_html_dom.php';
$html = file_get_html('http://somepage.com/');
$i=0;
$tabletitles = array(); /* Get the titles 'what' Cell Names */
$tabledetails = array(); /* Get the Details in 'whatdet' Cells */
$tables = $html->find('table[@class="infotable"]'); /* Where both reside in */
foreach($tables as $table) {
$titles = $table->find('td[@class="what"]');
$titlesd = $table->find('td[@class="whatdet"]');
foreach($titles as $title) {
/*UPDATE NOTICED A PROBLEM WITH a character like $ so I added */
/*will do the same in $titlesd if I can figure out how to get it */
$title1 = preg_replace('/([?#^&*()$\/])/', '\\', $title);
echo $title1; /*Changed from $title*/
if (isset($titlesd[$i])) /*this is just for testing*/
echo $titlesd[$i].' is either 0, empty, or not set at all';
/* WHAT I WANT is echo '<tr><td>'. $title .'</td><td>'. $titlesd[$i] . </td></tr>;*/
$i++;
}
}
我要尝试的是:
------------|----------
First name | Jim
----------- |---------
Last name | Bo
----------- |---------
Age |
----------- |---------
Sex | Rarely
----------- |---------
City |
----------- |---------
State | California
----------- |---------
但是我现在得到的是:
------------|----------
First name | Jim
----------- |---------
Last name | Bo
----------- |---------
Age | Rarely
----------- |---------
Sex | California
----------- |---------
City |
----------- |---------
State |
----------- |---------
我似乎无法弄清楚如何将 "blank" 分配给 $titlesd[$i]
或在循环中跳过它。所以,我不断得到不想要的结果。 (至少可以说)
所以,我再次恳求一位大师再给我上一堂宝贵的课。 谢谢..
使用
$titlesd = ($table->find('td[@class="whatdet"]')) ? $table->find('td[@class="whatdet"]') : "";
如果我没记错的话,这就是你想要做的:
require_once 'simpledom/simple_html_dom.php';
$html = file_get_html('http://somepage.com/');
foreach($html->find('table[@class="infotable"]') as $table) {
foreach($table->find('tr') as $line) {
$titles = $line->find('td[@class="what"]', 0);
$titlesd = $line->find('td[@class="whatdet"]', 0);
echo '<tr>'
.'<td>'.htmlspecialchars($titles).'</td>'
.'<td>'.htmlspecialchars($titlesd).'</td>'
.'</tr>';
}
}
我解释一下:
- 首先,我选择
<tr>
行,因为这是您要打印的输出内容 - 接下来,我尝试为当前
<tr>
行 上的标题和数据获取第一个匹配项 (index - 最后,我用
htmlspecialchars()
打印它们以避免&>'<"
出现问题
0
as 2nd parameter with ->find()
)
nb:因为 ->find()
与索引将 return NULL
如果没有找到,我让这段代码假设最坏情况下 echo $titlesd
会是 NULL
并且不显示任何内容
Blag 的回答很好,但可以更简单:
foreach($html->find('.infotable tr') as $tr){
echo $tr->find('td.what',0)->text();
echo $tr->find('td.whatdet',0)->text();
echo "\n";
}