Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要php编码特殊字符,而不是html标记,以便包含在wordpress扩展rss文件中_Php_Html_Xml_Wordpress_Rss - Fatal编程技术网

需要php编码特殊字符,而不是html标记,以便包含在wordpress扩展rss文件中

需要php编码特殊字符,而不是html标记,以便包含在wordpress扩展rss文件中,php,html,xml,wordpress,rss,Php,Html,Xml,Wordpress,Rss,我编写了一个脚本,将现有非wordpress站点的所有用户、博客和回复导出到wordpress扩展rss文件中,以便作为迁移的一部分导入到新的wordpress安装中。这一点很有效,直到它出现在一篇特定的博客文章中,并在一个法语或法语-加拿大短语中使用了一个特殊的标点符号 XML Parsing Error: not well-formed Location: http://example.com/wordpress_xml/export-to-wp.php Line Number 2000,

我编写了一个脚本,将现有非wordpress站点的所有用户、博客和回复导出到wordpress扩展rss文件中,以便作为迁移的一部分导入到新的wordpress安装中。这一点很有效,直到它出现在一篇特定的博客文章中,并在一个法语或法语-加拿大短语中使用了一个特殊的标点符号

XML Parsing Error: not well-formed
Location: http://example.com/wordpress_xml/export-to-wp.php
Line Number 2000, Column 270:* ... <i>l'art du d\uffffplacement</i> ... 

我删掉了上面的全部错误。将显示类似于逗号的字符,而不是\uffff。在php代码中,我将博客的html放在一个字符串中。我需要在不编码任何html标记的情况下对这种类型的字符进行编码,在进行了大量搜索之后,我画了一个空白。有人做过类似的事情吗?

在发现问题是关于口音的之后,我在php.net上找到了以下函数,它们适用于我的案例,我生成的导出文件很好地导入到wordpress博客中

function xmlentities($string) {
    // Function from: http://php.net/manual/en/function.htmlentities.php
    // Posted by: snevi at im dot com dot ve 22-Jul-2008 01:10
    $string = preg_replace('/[^\x09\x0A\x0D\x20-\x7F]/e', '_privateXMLEntities("$0")', $string);
    return $string;
}

function _privateXMLEntities($num) {
    // Function from: http://php.net/manual/en/function.htmlentities.php
    // Posted by: snevi at im dot com dot ve 22-Jul-2008 01:10
        $chars = array(
    128 => '&#8364;',
    130 => '&#8218;',
    131 => '&#402;',
    132 => '&#8222;',
    133 => '&#8230;',
    134 => '&#8224;',
    135 => '&#8225;',
    136 => '&#710;',
    137 => '&#8240;',
    138 => '&#352;',
    139 => '&#8249;',
    140 => '&#338;',
    142 => '&#381;',
    145 => '&#8216;',
    146 => '&#8217;',
    147 => '&#8220;',
    148 => '&#8221;',
    149 => '&#8226;',
    150 => '&#8211;',
    151 => '&#8212;',
    152 => '&#732;',
    153 => '&#8482;',
    154 => '&#353;',
    155 => '&#8250;',
    156 => '&#339;',
    158 => '&#382;',
    159 => '&#376;');
    $num = ord($num);
    return (($num > 127 && $num < 160) ? $chars[$num] : "&#".$num.";" );
} 

对于拉丁语-1,您可以使用以下方法轻松转义字符:

$html = preg_replace('/[\x80-\xFF]/e', '"&#x".dechex(ord("$0")).";"', $html);
对于UTF-8,它更涉及:

$html = preg_replace_callback("/(?!\w)\p{L}/u", "xmlent", $html);
function xmlent($m) {
    $str = mb_convert_encoding( $m[0] , "UCS-2BE", "UTF-8");
    return "&#x" . bin2hex($str) . ";";
}

好吧,我进一步研究了一下,我想补充一点,这个角色应该是一个e,上面有口音。这在原始站点上呈现良好,但在导出为xml时会出现抖动。我想这意味着我真正需要的是编码重音字符,而不是html标记……你的内容有哪些字符编码?@mario,不确定,但我自己解决了这个问题,见下文。有趣的是,在我提出这个问题之前,我一直被卡住。这件事一直发生在我身上……等等,这看起来比我的解决方案还要好。我猜这会处理所有字符,来自任何支持unicode的字母表?确实如此。它不依赖于固定列表,但应该能够处理所有Unicode字符。使用第二个版本,并可能应用$html=utf8\u encode$html;我应该这样做,因为我计划为其他想从同一个cms迁移到wordpress的人发布这个导出代码,我不知道他们可能会使用什么字符编码。还不如让它尽可能普遍地有用。谢谢然后还要注意使用mb_detect_编码来避免双重转换,preg_replace可能会被非UTF8字符串混淆。