gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:59:07 26 4
gpt4 key购买 nike

我编写了一个脚本,可以将现有(非 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 标签的情况下对这种类型的字符进行编码,经过大量搜索,我到目前为止还是一片空白。有人做过这样的事情吗?

最佳答案

对于 Latin-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) . ";";
}

关于php - 需要 php 来编码特殊字符而不是 html 标签,以便包含在 wordpress 扩展的 rss 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5362195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com