缩短末尾带有 ... 的字符串

发布于 2024-10-08 01:05:17 字数 36 浏览 0 评论 0原文

有没有官方的 PHP 函数可以做到这一点?这个动作叫什么?

Is there an official PHP function to do this? What is this action called?

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

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

发布评论

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

评论(4

攀登最高峰 2024-10-15 01:05:17

不,没有内置函数,但您当然可以构建自己的函数:

function str_truncate($str, $length) {
   if(strlen($str) <= $length) return $str;
   return substr($str, 0, $length - 3) . '...';
}

No, there is no built-in function, but you can of course build your own:

function str_truncate($str, $length) {
   if(strlen($str) <= $length) return $str;
   return substr($str, 0, $length - 3) . '...';
}
等待圉鍢 2024-10-15 01:05:17
function truncateWords($input, $numwords, $padding="") {
    $output = strtok($input, " \n");
    while(--$numwords > 0) $output .= " " . strtok(" \n");
    if($output != $input) $output .= $padding;
    return $output;
}

按单词截断

function truncateWords($input, $numwords, $padding="") {
    $output = strtok($input, " \n");
    while(--$numwords > 0) $output .= " " . strtok(" \n");
    if($output != $input) $output .= $padding;
    return $output;
}

Truncate by word

旧人哭 2024-10-15 01:05:17

不,PHP 没有内置的“截断”字符串函数(除非某个奇怪的扩展有这个函数,但不能保证观众/您将拥有这种插件——我不知道有哪个插件可以做到这一点) 。

我建议只为它编写一个简单的函数,例如:

<?php

function truncate($str, $len)
{
  if(strlen($str) <= $len) return $str;
  $str = substr($str, 0, $len) . "...";
  return $str;
}

?>

如果您想使用“暂停点”字符(带有三个点的单个字符;它是 unicode),请使用 HTML 实体 &哎呀;

No, PHP does not have a function built-in to "truncate" strings (unless some weird extension has one, but it's not guaranteed the viewers/you will have that sort of plugin -- I don't know of any that do).

I would recommend just writing a simple function for it, something like:

<?php

function truncate($str, $len)
{
  if(strlen($str) <= $len) return $str;
  $str = substr($str, 0, $len) . "...";
  return $str;
}

?>

And if you'd like to use a "suspension point" character (a single character with the three dots; it's unicode), use the HTML entity .

烂柯人 2024-10-15 01:05:17

我使用 wordwrapsubstrstrpos 确保它不会截断单词,或者分隔符前面没有空格。

function truncate($str, $length, $delimiter = '...') {
   $ww = wordwrap($str, $length, "\n");
   return substr($ww, 0, strpos($ww, "\n")).$delimiter;
}

$str = 'The quick brown fox jumped over the lazy dog.';
echo truncate($str, 25); 
// outputs 'The quick brown fox...' 
// as opposed to 'The quick brown fox jumpe...' when using substr() only

I use a combination of wordwrap, substr and strpos to make sure it doesn't cut off words, or that the delimiter is not preceded by a space.

function truncate($str, $length, $delimiter = '...') {
   $ww = wordwrap($str, $length, "\n");
   return substr($ww, 0, strpos($ww, "\n")).$delimiter;
}

$str = 'The quick brown fox jumped over the lazy dog.';
echo truncate($str, 25); 
// outputs 'The quick brown fox...' 
// as opposed to 'The quick brown fox jumpe...' when using substr() only
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文