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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题