【问题标题】:how to remove html tags after display xml content in php?在php中显示xml内容后如何删除html标签?
【发布时间】: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[&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 echo strip_tags(html_entity_decode($a)) ?>

【问题讨论】:

  • 您的代码应该可以工作,请参阅:eval.in/96248 &amp;nbsp; 是双重编码的,因此可能需要额外的 str_replace。

标签: php xml strip-tags


【解决方案1】:

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

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

因此,例如,您可以将自己代码中的 &lt;?php echo strip_tags($a) ?&gt; 更改为 &lt;?php echo strip_tags(html_entity_decode($a)) ?&gt;

【讨论】:

  • 你能解释一下你的答案吗?
  • @voodoo417 你觉得有什么需要解释的?
  • 读取 nodeValue 将返回解码后的实体。无需额外解码。
  • @DampeS8N “第一个版本”的答案没有任何意义。
猜你喜欢
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
相关资源
最近更新 更多