doushen4719 2014-02-20 05:01
浏览 39
已采纳

在PHP中输出由逗号分隔的HTML表格元素

My code strips td elements from an html table and outputs them in a single line.

Here is an html table my code extracts td values from:

<table width="100%"  border="0" cellspacing="1" cellpadding="0" bgcolor="#006699">
<tr align="center" class="tableRow1Font">
<td width="7%">WAITLIST</td>
<td width="5%">91630</td>
<td width="11%">
ACCY <A HREF="http://www.gwu.edu/~bulletin/ugrad/accy.html#2001" target="_blank">2001</A>
</td>
<td width="5%">10</td>
<td width="16%">Intro Financial Accounting</td>
<td width="6%">3.00</td>
<td width="8%"> Zou, Y</td>
<td width="8%"><A HREF="http://www.gwu.edu/~map/building.cfm?BLDG=DUQUES" target="_blank"
>DUQUES</a> 251</td>
<td width="13%">TR<br>09:35AM - 10:50AM</td>
<td width="14%">
01/13/14 - 04/28/14
</td>
<td width="7%">
</td>
</tr>
</table>

My code that takes these td values and outputs them on their own line. Keep in mind this is only one table of many in my .html file. Here is my code:

$html = file_get_html('badm.html');

$str = '';

foreach($html->find('table tr[align="center"]') as $e)
    $str .= strip_tags($e->innertext) . '<br>';
        echo $str;

The output looks like this (each line is a separate table in my badm.html file): Function Output

My question is this: How can I separate each td element with a single comma WHILE keeping them in the same format above (on their own line)?

  • 写回答

1条回答 默认 最新

  • duanfeng3879 2014-02-20 05:20
    关注
    $html = file_get_html('badm.html');
    $str = '';
    foreach($html->find('table tr[align="center"]') as $e)
    {
      foreach($e->find('td') as $f)
      {
       $str .= strip_tags($f->innertext) . ',';
      }
    $str = trim($str, ",");
    $str .= '<br>';
    }
    echo $str; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大