chunk_split() 破坏多字节字符

发布于 2024-10-11 22:01:18 字数 354 浏览 8 评论 0原文

当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霓裳挽歌倾城醉 2024-10-18 22:01:18

chunk_split 不是多字节安全的,并且没有本机 mb_chunk_split。

http://php.net/manual/en/function.chunk-split.php

下面是 php 文档中评论者提供的一个函数:

<?php

//from Peter from dezzignz.com 05-Apr-2010 11:30 @ php.net

function mbStringToArray ($str) {
    if (empty($str)) return false;
    $len = mb_strlen($str);
    $array = array();
    for ($i = 0; $i < $len; $i++) {
        $array[] = mb_substr($str, $i, 1);
    }
    return $array;
}

function mb_chunk_split($str, $len, $glue) {
    if (empty($str)) return false;
    $array = mbStringToArray ($str);
    $n = 0;
    $new = '';
    foreach ($array as $char) {
        if ($n < $len) $new .= $char;
        elseif ($n == $len) {
            $new .= $glue . $char;
            $n = 0;
        }
        $n++;
    }
    return $new;
}

?>

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:

<?php

//from Peter from dezzignz.com 05-Apr-2010 11:30 @ php.net

function mbStringToArray ($str) {
    if (empty($str)) return false;
    $len = mb_strlen($str);
    $array = array();
    for ($i = 0; $i < $len; $i++) {
        $array[] = mb_substr($str, $i, 1);
    }
    return $array;
}

function mb_chunk_split($str, $len, $glue) {
    if (empty($str)) return false;
    $array = mbStringToArray ($str);
    $n = 0;
    $new = '';
    foreach ($array as $char) {
        if ($n < $len) $new .= $char;
        elseif ($n == $len) {
            $new .= $glue . $char;
            $n = 0;
        }
        $n++;
    }
    return $new;
}

?>
め可乐爱微笑 2024-10-18 22:01:18

尝试转换前后的字符集,如下所示:

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

乖乖兔^ω^ 2024-10-18 22:01:18

在处理多字节字符时,正则表达式为 chunk_split() 提供了非常简洁和直接的替代。

模式细分:

~       #start pattern delimiter
.{8}    #match 8 of any non-newline character
\K      #forget previously matched characters
(?!$)   #not followed by the end of the string
~       #end pattern delimiter
u       #evaluate in multibyte mode

替换为
,但我将使用 \n 进行演示。

代码:(演示

$str = 'áâãäąăāæåߧśšşçćčźżžýųűů';

var_export(
    preg_replace(
        '~.{8}\K(?!$)~u',
        "\n",
        $str
    )
);

输出:

'áâãäąăāæ
åߧśšşçć
čźżžýųűů'

Regex offers a very succinct and direct replacement for chunk_split() when dealing with multibyte characters.

Pattern breakdown:

~       #start pattern delimiter
.{8}    #match 8 of any non-newline character
\K      #forget previously matched characters
(?!$)   #not followed by the end of the string
~       #end pattern delimiter
u       #evaluate in multibyte mode

Replace with <br> but I'll demonstrate with \n.

Code: (Demo)

$str = 'áâãäąăāæåߧśšşçćčźżžýųűů';

var_export(
    preg_replace(
        '~.{8}\K(?!$)~u',
        "\n",
        $str
    )
);

Output:

'áâãäąăāæ
åߧśšşçć
čźżžýųűů'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文