首页 > 解决方案 > U+FFFD 特殊字符被插入到 PHP 中的字符串中

问题描述

所以我试图<br>在PHP中给定字符串中遇到的最长单词前面添加一个标签。我正在使用的字符串也可能包含来自各种语言的字符,但所有字符串都以 UTF-8 编码。

// Here I'm prepending a <br> tag before the longest word in $words which is a subset of $string
substr_replace($string, "<br>", strpos($string , $words[$longest]) - 1, 0);

但是我注意到,当我用这行代码修改一串韩文或俄文文本时,会插入一个 U+FFFD 字符,甚至替换字符串中的一些字符。有人会知道为什么会发生这种情况吗?

谢谢

标签: phputf-8

解决方案


查看这个内置函数 nl2br [ http://php.net/manual/en/function.nl2br.php ]

它在集合中的未定义字符 [ https://en.wikipedia.org/wiki/Specials_(Unicode_block) ] 所以它有点像 null。

我会在存储之前 base64_encode [ http://php.net/manual/en/function.base64-encode.php ]和 base64_decode [ http://php.net/manual/en/function.base64-decode.php]提供数据。

或者,您可以尝试 utf8_encode [ http://us2.php.net/manual/en/function.utf8-encode.php ] 和 utf8_decode [ http://us2.php.net/manual/en/function.utf8-decode .php ]

还有 UTF-8 字符集(https://en.wikipedia.org/wiki/UTF-8


推荐阅读