html特殊字符utf-8返回空字符串

问题描述 投票:11回答:2

我正在使用.php RSS生成器,在此行尝试从数据库中获取数据时遇到问题:

<description><![CDATA[<?=htmlspecialchars(utf8_substr($row['texto'], 0, 100), ENT_QUOTES, 'utf-8') ?>...]]></description>

[某些条目显示很好,而其他条目则不会返回任何文本...对可能出问题的任何想法?

这是所有代码:

<?php

require('php/config.php');
require('php/db.php');
require('php/utils.php');

header("Content-type: application/xml");

$db = new TSQL('SELECT * FROM entradas WHERE estado = 1 ORDER BY fecha DESC LIMIT 20');
if ( $db->executeQuery() ) {

?><?='<?xml version="1.0" encoding="utf-8" ?>' ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Tu Secreto</title>
        <link>http://www.tusecreto.com.ar/</link>
        <description>TuSecreto / No se lo cuentes a nadie, contaselo a todos</description>
        <language>es-ar</language>
        <copyright>TuSecreto (C) 2005-<?php print strftime("%Y", time()); ?></copyright>
        <lastBuildDate><?=strftime("%a, %d %b %Y %H:%M:%S ", $row['fecha']) ?></lastBuildDate>
        <atom:link href="http://www.tusecreto.com.ar/rss.php" rel="self" type="application/rss+xml" />
        <docs>http://www.tusecreto.com.ar/rss.php</docs>
        <generator>TuSecreto RSS Generator v1.0</generator>
        <ttl>10</ttl>
        <? while ($row = $db->getRow(MYSQL_ASSOC)) { ?>
        <item>
            <title><?=($row['sexo'] == MUJER)?'Mujer':'Hombre' ?> | <?=$row['edad'] ?> <?="A\xC3\xB1os" ?></title>
            <description><![CDATA[<?=htmlspecialchars(utf8_substr($row['texto'], 0, 100), ENT_QUOTES, 'utf-8') ?>...]]></description>
            <link>http://www.tusecreto.com.ar/<?=$row['id'] ?></link>
            <guid isPermaLink="true">http://www.tusecreto.com.ar/<?=$row['id'] ?></guid>
            <pubDate><?=strftime("%a, %d %b %Y %H:%M:%S ", $row['fecha']) ?></pubDate>
        </item>
        <?php } ?>
    </channel>
</rss>

这是一个返回空字符串的结果:

una vez en el colectivo(个人圣淘沙),我要和您一起去! se mataron de la risa todos!哈斯塔埃尔科莱蒂维罗! Pasalo y comento con mi Facebook。 E.P。

php mysql utf-8 htmlspecialchars
2个回答
16
投票

您的代码使用htmlspecialchars($string, ENT_QUOTES, 'utf-8')。引自manpage

如果输入字符串中包含无效的代码单元序列,给定的编码将返回一个空字符串,除非设置了ENT_IGNORE或ENT_SUBSTITUTE标志。


0
投票

这仍然没有解决,我最近找到了一个解决类似问题的解决方案,异常字符会使函数打印一个空字符串。因此,我打算放置我的输入内容。

在标志区域中,添加“ | ENT_SUBSTITUTE”并将编码类型更改为“ cp1252”。 ENT_SUBSTITUTE标志将确保替换所有无法识别的字符,而不是创建一个空字符串。但是,编码类型“ cp1252”是Windows特定的,如果不起作用(https://www.php.net/manual/en/function.htmlspecialchars.php),建议在手册页上查看其他类型。 (我认为这种编码对我有用的原因是因为我的服务器在Windows IIS上运行)

© www.soinside.com 2019 - 2024. All rights reserved.