转换 ' (') 以在 RSS URL 中使用

Convert ’ (’) for use in RSS URL

我正在从 Wordpress 中的 table 中提取图像 URL。

数据库中的URL是这样的:Power-behind-F1’s-winning-global-drive.jpg字段是UTF8 Unicode

将其拉出以生成 RSS 提要时,结果显示为 Power-behind-F1’s-winning-global-drive.jpg

我可以将它转换为一个 html 实体 utf8_encode(htmlentities($url, ENT_QUOTES,'utf-8')) 产生 Power-behind-F1’s-winning-global-drive.jpg。然而,这没有通过 RSS 验证:

我觉得我已经尝试了所有组合 utf8_encode/htmlentities/urlencode/mb_convert_encoding,但我 运行 没有想法!

到目前为止已解决

$imageUrl = urlencode($theRawUrl);
$imageUrl = str_replace(array( '%2F', '%5C' ), "/", $imageUrl);
$imageUrl = str_replace("%3A", ":", $imageUrl);

恶心,但它有效...

编辑:

我的另一个修复是:

iconv("UTF-8", "ISO-8859-1//IGNORE//TRANSLIT", $content)

由于没有代码可看,我只能分享一些提示。

  • UTF-8 是当今事实上的标准。遗留应用程序是使用任何其他编码的唯一原因,但似乎并非如此。坚持使用 UTF-8,不要费心转换为任何其他编码:它毫无意义,只会毁掉您的数据。

  • 转义总是上下文相关的,不是普遍的真理。在数据似乎可以工作之前,不要对数据应用随机转义函数,除非您想破坏它。

  • 不要通过字符串连接生成 XML:它没有任何优势,并且最终会生成格式错误的 XML(实际情况如此)。 Let PHP do the hard work for you. (Use SimpleXML 如果不确定。)