首页 > 解决方案 > PHP 解析 XML 内容而不循环超链接

问题描述

我有一个结构如下的 xml 文件:

<channel>
 <title>abc</title>
 <link>domain.com</link>
 <description>Bla bla.</description>
<item>
  <title>xyz </title>
  <link>domain.com/</link>
<description>
  <table border="1" width="100%"><tr><th colspan="2"></th><th>P</th><th>W</th><th>D</th><th>L</th><th>GF</th><th>GA</th><th>Dif</th><th>Pts</th></tr><tr><td width="7%">1</td><td width="27%"><a target="_blank" href="domain[dot]com/new-york/"/>New York</td><td width="7%"><center>12</center></td><td width="7%"><center>8</center></td><td width="7%"><center>2</center></td><td width="7%"><center>2</center></td><td width="7%"><center>17</center></td><td width="7%"><center>10</center></td><td width="7%"<center>+7</center></td><td width="7%"><center>26</center></td></tr><tr><td width="7%">2</td><td width="27%"><a target="_blank" href="domain[dot]com/lon-don/"/>London</td><td width="7%"><center>12</center></td><td width="7%"><center>6</center></td><td width="7%"><center>4</center></td><td width="7%"><center>2</center></td><td width="7%"><center>22</center></td><td width="7%"><center>12</center></td><td width="7%"><center>+10</center></td><td width="7%"><center>22</center></td></tr></table><br/>
</description>

我用这段代码来解析table dataPHP,我成功了:

$url = "link to the above xml file";
$xml = simplexml_load_file($url);
foreach($xml->channel->item as $item){

    $desc = html_entity_decode((string)$item->description);
    $descXML = simplexml_load_string('<desc>'.$desc.'</desc>');
    $html = $descXML->table->asXML();
    $html .= "<hr />";          
    echo $html;
}

但是,它还包括table data/中的超链接array values,它们是domain[dot]com/newyork/domain[dot]com/london/同时输出。

我期待的是我想exclude the hyperlinks在输出中,这意味着我只需要纯文本,例如Lon DonorNew York等​​等。

请在输出中没有超链接。

谢谢,

标签: php

解决方案


因为您只是在中显示整个表 XML

$html = $descXML->table->asXML();

这包含表的所有标记,如果您只想要一些表数据,您需要做的是进一步处理它以提取该数据......

$xml = simplexml_load_file($url);
foreach($xml->item as $item){

     $desc = html_entity_decode((string)$item->description);
     $descXML = simplexml_load_string('<desc>'.$desc.'</desc>');
     // Loop over each row of the table
     foreach ( $descXML->table->tr as $row ) {
        // If there are td elements
        if ( isset($row->td) )  {
            // Extract the value from the second td element, convert to a string and trim the result
            $html = trim((string)($row->td[1]));
            $html .= "<hr />";
            echo $html;

        }
    }
}

如果您想要<tr>除标签之外的所有 XML <a>,您可以取消设置它(假设它会一直存在)...

     foreach ( $descXML->table->tr as $row ) {
        // If there are td elements
        if ( isset($row->td) )  {
            unset($row->td[1]->a);
            $html = $row->asXML(). "<hr />";
            echo $html;

        }
    }

推荐阅读