在 PHP 中缩短/截断 UTF8 字符串

发布于 2024-11-03 15:16:13 字数 69 浏览 0 评论 0原文

我需要一个良好的快速函数,可以将字符串缩短到支持 UTF8 的设定长度。在末尾添加尾随“...”是一个优点。有人可以帮忙吗?

I need a good fast function that shortens strings to a set length with UTF8 support. Adding trailing '...' at ends is a plus. Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

自由如风 2024-11-10 15:16:13

假设安装了 mb_* 功能。

function truncate($str, $length, $append = '…') {
  $strLength = mb_strlen($str);

  if ($strLength <= $length) {
     return $str;
  }

  return mb_substr($str, 0, $length) . $append;
}

键盘

请记住,这将添加一个字符(省略号)。如果您希望将 $append 包含在截断的长度中,只需从您截断的字符串长度中减去 mb_strlen($append) 即可。

显然,这也会打断单词的中间部分。

更新

这是一个可以选择保留整个单词的版本...

function truncate($str, $length, $breakWords = TRUE, $append = '…') {
  $strLength = mb_strlen($str);

  if ($strLength <= $length) {
     return $str;
  }
  
  if ( ! $breakWords) {
       while ($length < $strLength AND preg_match('/^\pL$/', mb_substr($str, $length, 1))) {
           $length++;
       }
  }

  return mb_substr($str, 0, $length) . $append;
}

CodePad

如果第三个参数为 TRUE,它将保留第一个非字母字符之前的所有字母字符。

Assuming mb_* functions installed.

function truncate($str, $length, $append = '…') {
  $strLength = mb_strlen($str);

  if ($strLength <= $length) {
     return $str;
  }

  return mb_substr($str, 0, $length) . $append;
}

CodePad.

Keep in mind this will add one character (the elipsis). If you want the $append included in the length that is truncated, just minus the mb_strlen($append) from the length of the string you chop.

Obviously, this will also chop in the middle of words.

Update

Here is a version that can optionally preserve whole words...

function truncate($str, $length, $breakWords = TRUE, $append = '…') {
  $strLength = mb_strlen($str);

  if ($strLength <= $length) {
     return $str;
  }
  
  if ( ! $breakWords) {
       while ($length < $strLength AND preg_match('/^\pL$/', mb_substr($str, $length, 1))) {
           $length++;
       }
  }

  return mb_substr($str, 0, $length) . $append;
}

CodePad.

It will preserve all letter characters up to the first non letter character if the third argument is TRUE.

乖乖兔^ω^ 2024-11-10 15:16:13

我猜你需要截断文本,所以这可能会有所帮助:

if (!function_exists('truncate_string')) {
    function truncate_string($string, $max_length) {
        if (mb_strlen($string, 'UTF-8') > $max_length){
            $string = mb_substr($string, 0, $max_length, 'UTF-8');
            $pos = mb_strrpos($string, ' ', false, 'UTF-8');
            if($pos === false) {
                return mb_substr($string, 0, $max_length, 'UTF-8').'…';
            }
            return mb_substr($string, 0, $pos, 'UTF-8').'…';
        }else{
            return $string;
        }
    }
}

这类似于 @alex 刚刚发布的内容,但它不会破坏单词。

I guess you need to truncate text, so this may be helpful:

if (!function_exists('truncate_string')) {
    function truncate_string($string, $max_length) {
        if (mb_strlen($string, 'UTF-8') > $max_length){
            $string = mb_substr($string, 0, $max_length, 'UTF-8');
            $pos = mb_strrpos($string, ' ', false, 'UTF-8');
            if($pos === false) {
                return mb_substr($string, 0, $max_length, 'UTF-8').'…';
            }
            return mb_substr($string, 0, $pos, 'UTF-8').'…';
        }else{
            return $string;
        }
    }
}

This is something like @alex just posted, but it does not break words.

楠木可依 2024-11-10 15:16:13

试试这个:

$length = 100;
if(mb_strlen($text, "utf-8") > $length){
$last_space = mb_strrpos(mb_substr($text, 0, $length, "utf-8"), " ", "utf-8");
$text = mb_substr($text, 0, $last_space, "utf-8")." ...";}

干杯...

Try this:

$length = 100;
if(mb_strlen($text, "utf-8") > $length){
$last_space = mb_strrpos(mb_substr($text, 0, $length, "utf-8"), " ", "utf-8");
$text = mb_substr($text, 0, $last_space, "utf-8")." ...";}

Cheers...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文