PHP在元素之间回显逗号

时间:2014-02-19 02:35:44

标签: php parsing dom html-table echo

我的代码从html表中提取某些元素并列出它们:

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

输出如下:

OPEN 91212 ACCY 2001 12 Intro Financial Accounting 3.00 Zou, Y 1957 E B12 TR02:20PM - 03:35PM 01/13/14 - 04/28/14 

以下是这些项目的HTML表格:

 <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>

如何更改我的代码以回显逗号以分隔每个元素? 我希望它看起来像这样:

OPEN, 91212, ACCY 2001, 12, Intro Financial Accounting, 3.00, Zou, Y, 1957 E B12, TR02:20PM - 03:35PM, 01/13/14 - 04/28/14 

3 个答案:

答案 0 :(得分:3)

尝试

$html->find('tr[align=center] td') 

而是遍历每个td,并用逗号

替换br标签

答案 1 :(得分:1)

echo strip_tags($e->innertext) . ', ';

答案 2 :(得分:0)

foreach($html->find('tr[align=center]') as $e){ 
     $str .= strip_tags($e->innertext). ",";
}
$str = trim($str, ",");//delete last ","
echo $str;