PHPQuery- Select table 列到一个数组
PHPQuery- Select table columns into an array
我有一个 HTML 文件,其中 table 是这样的:
<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>
我需要根据 class id 将每个 <td>
放入一个单独的数组元素中。
我正在使用 PHPQuery。我试过了
$table = $all['tr.dselbkg'];
$columns = $table['td'];
但它所做的是将所有列作为一个字符串。因此 $columns[0]
打印出所有列,而 $columns[1]
为空白。
如何将单个 <td>
作为数组中的单个元素?
使用 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);
我有一个 HTML 文件,其中 table 是这样的:
<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>
我需要根据 class id 将每个 <td>
放入一个单独的数组元素中。
我正在使用 PHPQuery。我试过了
$table = $all['tr.dselbkg'];
$columns = $table['td'];
但它所做的是将所有列作为一个字符串。因此 $columns[0]
打印出所有列,而 $columns[1]
为空白。
如何将单个 <td>
作为数组中的单个元素?
使用 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);