gpt4 book ai didi

php - 如何在不破坏 unicode 的情况下截断具有 unicode 字符的单词?

转载 作者:行者123 更新时间:2023-12-04 04:56:07 25 4
gpt4 key购买 nike

我有一个像“Stefan Äöüéèêë”这样的词。我想在 20 个字符后截断这个字符串......但它给了我像 Stefan Äöüéèê� 。
我为此使用以下代码。

$string = "Stefan Äöüéèêë";
if(strlen($string) > 20){
$string = substr($string, 0, 20) . '...';
}

如何获得安全截断的字符串。

最佳答案

使用它的多字节版本:
http://php.net/manual/de/function.mb-substr.php

此外,这里有一个非常方便的 unicode 不安全方法列表:http://www.phpwact.org/php/i18n/utf-8

注意:strlen 确实返回了错误的值

关于php - 如何在不破坏 unicode 的情况下截断具有 unicode 字符的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16729814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com