用重音切割一个字符串[重复]

时间:2021-11-01 21:35:55

Possible Duplicate:
Truncate a multibyte String to n chars

可能重复:将多字节字符串截断为n个字符

Hello,

你好,

I'm developing a site in French and I created a function that cuts string after X number of characters. It works great, but when the last character has an accent, it has the (?) instead of the character.

我正在用法语开发一个网站,我创建了一个在X个字符后剪切字符串的函数。它工作得很好,但是当最后一个字符有重音时,它有(?)而不是字符。

Here is my function

这是我的功能

function neat_trim3($str, $n, $delim='...') {
    $len = strlen($str);
    if ($len > $n) {
        $tocut = $len-$n;
        $output = substr($str,0,-$tocut);
        return $len.' - '.$output.$delim;
    }
    else {
        return $str;
    }
}

Example

$str = "C'est une soirée privé ce soir";
echo eat_trim3($str, 15 , '...' );   
// GIVES ME             C'est une soir�...

The rest of the page echos the page perfectly, I can even echo the same string without the cut and it works great.

页面的其余部分完美地回应了页面,我甚至可以在没有剪切的情况下回显相同的字符串,并且效果很好。

Any help appreciated.

任何帮助赞赏。

Thanks.

谢谢。

1 个解决方案

#1


16  

Use the mb_substr and mb_strlen functions. It doesn't work because you are using UTF-8, which has multi-byte characters and you cut the in the middle.

使用mb_substr和mb_strlen函数。它不起作用,因为你使用UTF-8,它有多字节字符,你在中间切。

#1


16  

Use the mb_substr and mb_strlen functions. It doesn't work because you are using UTF-8, which has multi-byte characters and you cut the in the middle.

使用mb_substr和mb_strlen函数。它不起作用,因为你使用UTF-8,它有多字节字符,你在中间切。