【发布时间】:2014-01-29 20:29:28
【问题描述】:
在php中显示xml内容后如何去除html标签?我也尝试过strip_tags,但无法删除 html 标签?
php 代码
<?php
$doc = new DOMDocument();
$doc->load( 'aboutus.xml' );
$items = $doc->getElementsByTagName( "item" );
foreach( $items as $item )
{
$contents = $item->getElementsByTagName( "content" );
$content = $contents->item(0)->nodeValue;
$a= $content;
}
?>
在 php 页面中显示 xml 内容
<div style="width:50%" align="center"> <?php echo strip_tags($a) ?></div>
**aboutus.xml 文件**
<?xml version="1.0" encoding="UTF-8"?>
<item><pubDate>Sat, 05 Oct 2013 14:43:39 +0500</pubDate>
<title><![CDATA[Who We Are]]></title>
<url><![CDATA[about-us]]></url>
<meta><![CDATA[]]></meta>
<metad><![CDATA[]]></metad>
<menu><![CDATA[About Us]]></menu>
<menuOrder><![CDATA[0]]></menuOrder>
<menuStatus><![CDATA[]]></menuStatus>
<template><![CDATA[template.php]]></template>
<parent><![CDATA[]]></parent>
<content>
<![CDATA[<h1>Who We Are</h1><p>&nbsp;</p>
<h2>About Desk</h2>
<p>&nbsp;</p>
<p><span style=\"text-align: justify;\">
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry\'s standard dummy text ever since
the 1500s, when an unknown printer took a galley of type and scrambled
it to make a type specimen book. It has survived not only five centuries,
but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing
Lorem Ipsum passages, and more recently with desktop publishing software
PageMaker including versions of Lorem Ipsum.</span></p>
]]>
</content>
</item>
显示内容截图
解决方案
<?php echo strip_tags(html_entity_decode($a)) ?>
【问题讨论】:
-
您的代码应该可以工作,请参阅:eval.in/96248
&nbsp;是双重编码的,因此可能需要额外的 str_replace。
标签: php xml strip-tags