chunk_split() 破坏多字节字符
当我使用 chunk_split() 函数时,它会破坏我的重音字符和特殊字符。我该如何纠正这个问题?
这是我的 PHP 代码。
if(count($text) > 0) {
$text = implode(' ', $text);
echo chunk_split($text, 8, '<br />');
}
被破坏的重音字符。
&a
mp; Post
er ÀÁ�
�ÃÄÅ�
�áâã�
�åÒÓ�
�ÕÖØ�
�óôõ�
�øÈÉ�
�Ëéè�
�ëÇç�
�ÍÎÏ�
�íîï�
�ÚÛÜ�
�úûü�
�Ññ
When I use the chunk_split()
function, it ruins my accented characters and special characters. How can I correct this problem?
Here is my PHP code.
if(count($text) > 0) {
$text = implode(' ', $text);
echo chunk_split($text, 8, '<br />');
}
Ruined accent characters.
&a
mp; Post
er ÀÁ�
�ÃÄÅ�
�áâã�
�åÒÓ�
�ÕÖØ�
�óôõ�
�øÈÉ�
�Ëéè�
�ëÇç�
�ÍÎÏ�
�íîï�
�ÚÛÜ�
�úûü�
�Ññ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
chunk_split 不是多字节安全的,并且没有本机 mb_chunk_split。
http://php.net/manual/en/function.chunk-split.php
下面是 php 文档中评论者提供的一个函数:
chunk_split isn't multibyte safe and there isn't a native mb_chunk_split.
http://php.net/manual/en/function.chunk-split.php
Here is a function to do that from a commenter in the php docs:
尝试转换前后的字符集,如下所示:
https: //www.php.net/manual/en/function.chunk-split.php#99316
Try converting the character set before and after as seen here:
https://www.php.net/manual/en/function.chunk-split.php#99316
在处理多字节字符时,正则表达式为
chunk_split()
提供了非常简洁和直接的替代。模式细分:
替换为
,但我将使用\n
进行演示。代码:(演示)
输出:
Regex offers a very succinct and direct replacement for
chunk_split()
when dealing with multibyte characters.Pattern breakdown:
Replace with
<br>
but I'll demonstrate with\n
.Code: (Demo)
Output: