【问题标题】:How to decode html tags in xml using php如何使用php解码xml中的html标签
【发布时间】:2015-07-22 01:50:42
【问题描述】:

我尝试解码 xml 中的 html 标签,但 html 标签的功能不起作用。 这是我的代码:

<?php

$sample = '<p>&nbsp Sample</p>
           <p>Sample 2</p>';

header('Content-type: text/xml');
$output = '<rss version="2.0">';
$output .= '<channel>';
$output = '<description>.utf8_encode(html_entity_decode($sample)).</description>';
echo($output);
$output .= '</channel>';
$output .= '</rss>';
?>

输出是纯文本。 &lt;p&gt; 标签的功能不起作用。当我删除utf8_encode 时,&amp;nbsp; 会出错。

【问题讨论】:

  • &amp;nbsp 是无效实体。您在关闭 channelrss 元素之前输出。

标签: php html xml


【解决方案1】:

试试这个

<?php
$sample = '<p>Sample</p><p>Sample 2</p>';

header('Content-type: text/xml');
$output = '<rss version="2.0">';
$output .= '<channel>';
$output .= '<description>'. strip_tags(utf8_encode(html_entity_decode($sample))).'</description>';
$output .= '</channel>';
$output .= '</rss>';
echo($output);

结果:

<rss version="2.0">
    <channel>
        <description>
            <p>Sample</p>
            <p>Sample 2</p>
        </description>
    </channel>
</rss>

你需要在字符串上分离 PHP 函数。

【讨论】:

  • 另外,您可能不想在 ... 行中直接分配给 $output。你可能想再次使用 .=
  • 对不起。我错误地编码了我的代码。我使用该代码,但我有相同的输出。描述是显示纯文本。
  • @ChristianBeltran 你必须去掉你不想要的标签
  • @JosuaMarcelChrisano 当我使用 strip_tags 时输出相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 2011-05-18
  • 1970-01-01
  • 2013-07-07
  • 2023-03-26
  • 2013-11-19
  • 1970-01-01
相关资源
最近更新 更多