我有一个像这样的表格的HTML文件:
<tr valign="top" class="dselbkg" onMouseOver="this.className='selbkg'" onMouseOut="this.className='dselbkg'" >
<td height="20" align="center">1</td>
<td height="20"><div align="center">16-12-2014</div></td>
<td ><div align="center">1ST<br>
(10:0 AM - 1:0 PM)</div></td>
<td ><div align="center">BE2105 </div></td>
<td >PROGRAMMING IN C</td>
</tr>
<tr valign="top" class="dselbkg" onMouseOver="this.className='selbkg'" onMouseOut="this.className='dselbkg'" >
<td height="20" align="center">2</td>
<td height="20"><div align="center">18-12-2014</div></td>
<td ><div align="center">1ST<br>
(10:0 AM - 1:0 PM)</div></td>
<td ><div align="center">BS1101 </div></td>
<td >MATHEMATICS - I</td>
</tr>
<tr valign="top" class="dselbkg" onMouseOver="this.className='selbkg'" onMouseOut="this.className='dselbkg'" >
<td height="20" align="center">3</td>
<td height="20"><div align="center">20-12-2014</div></td>
<td ><div align="center">1ST<br>
(10:0 AM - 1:0 PM)</div></td>
<td ><div align="center">HM3101 </div></td>
<td >COMMUNICATIVE ENGLISH</td>
</tr>
我需要根据类id将每个<td>
放入一个单独的数组元素中。
我正在使用PHPQuery。我试过了
$table = $all['tr.dselbkg'];
$columns = $table['td'];
但它的作用是将所有列都作为单个字符串。因此$columns[0]
打印出所有列,$columns[1]
为空。
如何将单个<td>
作为数组中的单个元素?
答案 0 :(得分:1)
使用PHPQuery编辑:
<?php
include('phpQuery.php');
$htmlString = '<tr valign="top" class="dselbkg" onMouseOver="this.className=\'selbkg\'" onMouseOut="this.className=\'dselbkg\'" >
<td height="20" align="center">1</td>
<td height="20"><div align="center">16-12-2014</div></td>
<td ><div align="center">1ST<br>
(10:0 AM - 1:0 PM)</div></td>
<td ><div align="center">BE2105 </div></td>
<td >PROGRAMMING IN C</td>
</tr>
<tr valign="top" class="dselbkg" onMouseOver="this.className=\'selbkg\'" onMouseOut="this.className=\'dselbkg\'" >
<td height="20" align="center">2</td>
<td height="20"><div align="center">18-12-2014</div></td>
<td ><div align="center">1ST<br>
(10:0 AM - 1:0 PM)</div></td>
<td ><div align="center">BS1101 </div></td>
<td >MATHEMATICS - I</td>
</tr>
<tr valign="top" class="dselbkg" onMouseOver="this.className=\'selbkg\'" onMouseOut="this.className=\'dselbkg\'" >
<td height="20" align="center">3</td>
<td height="20"><div align="center">20-12-2014</div></td>
<td ><div align="center">1ST<br>
(10:0 AM - 1:0 PM)</div></td>
<td ><div align="center">HM3101 </div></td>
<td >COMMUNICATIVE ENGLISH</td>
</tr> ';
$doc = phpQuery::newDocumentHTML($htmlString);
foreach (pq('tr.dselbkg') as $row){
$columns = array();
foreach(pq('td',$row) as $td) $columns[] = $td->nodeValue;
$tableRows[] = $columns;
}
print_r($tableRows);
/*
Array
(
[0] => Array
(
[0] => 1
[1] => 16-12-2014
[2] => 1ST
(10:0 AM - 1:0 PM)
[3] => BE2105
[4] => PROGRAMMING IN C
)
[1] => Array
(
[0] => 2
[1] => 18-12-2014
[2] => 1ST
(10:0 AM - 1:0 PM)
[3] => BS1101
[4] => MATHEMATICS - I
)
[2] => Array
(
[0] => 3
[1] => 20-12-2014
[2] => 1ST
(10:0 AM - 1:0 PM)
[3] => HM3101
[4] => COMMUNICATIVE ENGLISH
)
)
*/
您可以轻松使用simple html dom查询您的HTML。
以下是如何使用已解析的td
元素构建数组的示例。
<?php
include('simple_html_dom.php');
$htmlString = '<tr valign="top" class="dselbkg" onMouseOver="this.className=\'selbkg\'" onMouseOut="this.className=\'dselbkg\'" >
<td height="20" align="center">1</td>
<td height="20"><div align="center">16-12-2014</div></td>
<td ><div align="center">1ST<br>
(10:0 AM - 1:0 PM)</div></td>
<td ><div align="center">BE2105 </div></td>
<td >PROGRAMMING IN C</td>
</tr>
<tr valign="top" class="dselbkg" onMouseOver="this.className=\'selbkg\'" onMouseOut="this.className=\'dselbkg\'" >
<td height="20" align="center">2</td>
<td height="20"><div align="center">18-12-2014</div></td>
<td ><div align="center">1ST<br>
(10:0 AM - 1:0 PM)</div></td>
<td ><div align="center">BS1101 </div></td>
<td >MATHEMATICS - I</td>
</tr>
<tr valign="top" class="dselbkg" onMouseOver="this.className=\'selbkg\'" onMouseOut="this.className=\'dselbkg\'" >
<td height="20" align="center">3</td>
<td height="20"><div align="center">20-12-2014</div></td>
<td ><div align="center">1ST<br>
(10:0 AM - 1:0 PM)</div></td>
<td ><div align="center">HM3101 </div></td>
<td >COMMUNICATIVE ENGLISH</td>
</tr> ';
$html = str_get_html($htmlString);
foreach($html->find('tr.dselbkg') as $tr){
$columns = array();
foreach($tr->find('td') as $td)
$columns[] = $td->outertext; // outertext if you want the full td tag
// $columns[] = $td->innertext // innertext if you just want the text inside the td tag
$tablerows[]=$columns;
}
print_r($tablerows);
/*
Array
(
[0] => Array
(
[0] => <td height="20" align="center">1</td>
[1] => <td height="20"><div align="center">16-12-2014</div></td>
[2] => <td ><div align="center">1ST<br> (10:0 AM - 1:0 PM)</div></td>
[3] => <td ><div align="center">BE2105 </div></td>
[4] => <td >PROGRAMMING IN C</td>
)
[1] => Array
(
[0] => <td height="20" align="center">2</td>
[1] => <td height="20"><div align="center">18-12-2014</div></td>
[2] => <td ><div align="center">1ST<br> (10:0 AM - 1:0 PM)</div></td>
[3] => <td ><div align="center">BS1101 </div></td>
[4] => <td >MATHEMATICS - I</td>
)
*/
$html->clear();
unset($html);