我有一个结构如下的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>
我使用这段代码来解析PHP中的table data
,我成功了:
$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 Don
或New York
之类的纯文本。
请在输出中没有超链接。
谢谢
答案 0 :(得分:0)
由于您只是在
中显示整个表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>
标记之外的所有<a>
XML,则可以将其取消设置(假设它将始终存在)...
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;
}
}