php - 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 字符,甚至替换字符串中的一些字符。有人会知道为什么会发生这种情况吗?
谢谢
解决方案
查看这个内置函数 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)
推荐阅读
- php - Mysqli db 到 db 复制和奇怪的字符集行为
- java - 更改 TextInputLayout 提示行为
- java - zk 如何使用 mvvm 为每个或模板在 zul 中生成组件?
- javascript - 从 Http (ng2) 升级到 HttpClient (ng6) 后登录请求的未定义响应
- python - 在不打开终端的情况下运行 Python 脚本
- html - Bootstrap 在一行中设置 4 个元素
- android - why some images showing blank/black in imageView
- c# - 使用 WebClient 从 xml 反序列化到 C# 中的对象时出现意外字符
- android - 为什么我的 Serializable UID 没有被使用?
- javascript - Firebase 在“downloadURL”上返回“未定义”