php - 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 data
PHP,我成功了:
$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
orNew York
等等。
请在输出中没有超链接。
谢谢,
解决方案
因为您只是在中显示整个表 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;
}
}
推荐阅读
- javascript - 通过使用 javascript 创建批处理,一次从图片库中下载多张图片
- assembly - 0x00400030 处的运行时异常:地址未在双字边界 0x10010004 上对齐
- ios - 将 pod 嵌入到静态库 iOS
- hygieia - Hygieia:github 小部件下的问题-在 Repo 类型下看不到任何内容
- selenium-webdriver - Selenium VBA FindElement 对象所需的错误
- debian - 制作deb文件
- amazon-web-services - 阻止某些国家 X-forwarded-for - Nginx
- user-interface - Firefox 遍历树失败,进一步调查显示树层次结构混乱
- python-3.x - Pickle 不能腌制 _thread.lock 对象
- python - TypeError:不支持的操作数类型/:'float'和'csr_matrix'