如何在php中显示xml内容后删除html标签?

时间:2014-01-29 20:29:28

标签: php xml strip-tags

如何在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[&lt;h1&gt;Who We Are&lt;/h1&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;About Desk&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style=\&quot;text-align: justify;\&quot;&gt;
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry\&#039;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.&lt;/span&gt;&lt;/p&gt;
]]>
</content>

</item>

显示内容屏幕截图

enter image description here

溶液

<?php echo strip_tags(html_entity_decode($a)) ?>

1 个答案:

答案 0 :(得分:0)

您看到的HTML实际上不是HTML。 <>字符正在更改为实体&lt;&gt;。您可以使用html_entity_decode()更改它们。

然后,您可以使用strip_tags()删除HTML,或者您可以保留它,因为它现在可以像HTML一样工作。

因此,举例来说,您可以将自己代码中的<?php echo strip_tags($a) ?>更改为<?php echo strip_tags(html_entity_decode($a)) ?>