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

问题描述:

如何在php中显示xml内容后删除html标签?我也试过strip_tags,但不工作,以消除html标签?如何在php中显示xml内容后删除html标签?

PHP代码在PHP页面

<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> 

显示内容

<?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; 
} 
?> 

显示的XML内容截图

enter image description here

解决方案

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

您的代码应该工作,请参阅:https://eval.in/96248的'' 为双编码,所以可能需要额外的str_replace。 – ThW

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

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

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

+0

你能解释你的答案吗? – voodoo417

+1

@ voodoo417你觉得需要更多解释吗? – DampeS8N

+0

读取nodeValue将返回解码的实体。不需要额外的解码。 – ThW