PHP - 如何截断由 foreach 循环形成的字符串?

发布于 2024-11-09 06:08:27 字数 627 浏览 4 评论 0原文

假设我的视图中有以下循环

foreach ($value as $row):
     echo $row['name'] . ', ';
endforeach;

这会在浏览器中输出这样的字符串

盖迪、李、尼尔、皮尔特、亚历克斯

我想知道是否有人可以建议一种在 n 个字符处截断此字符串的方法,例如

盖迪、李、内...

由于字符串是从循环中输出的,所以我不确定如何在这个 foreach 周围包装一个截断函数。

感谢您的帮助!

示例截断函数

function truncate_text($text, $nbrChar, $append='...') {
     if(strlen($text) > $nbrChar) {
          $text = substr($text, 0, $nbrChar);
          $text .= $append;
     }
     return $text;
}

Say I have the following loop in my view

foreach ($value as $row):
     echo $row['name'] . ', ';
endforeach;

This outputs a string like this in my browser

Geddy, Lee, Neil, Peart, Alex,

I wonder if anyone can suggest a method to truncate this string at n characters, for example

Geddy, Lee, Ne...

Since the string is being output from the loop I am unsure how to wrap a truncate function around this foreach.

Thanks for helping!

sample truncate function

function truncate_text($text, $nbrChar, $append='...') {
     if(strlen($text) > $nbrChar) {
          $text = substr($text, 0, $nbrChar);
          $text .= $append;
     }
     return $text;
}

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

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

发布评论

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

评论(4

从此见与不见 2024-11-16 06:08:27

为什么不将行值保存到变量中并截断该变量并仅回显该变量?

var $str = "";
foreach ($value as $row):
     $str .= $row['name'] . ', ';
endforeach;

echo truncate_text($str, 'whatever');

Why not save the row values to a variable and truncate that variable and just echo that?

var $str = "";
foreach ($value as $row):
     $str .= $row['name'] . ', ';
endforeach;

echo truncate_text($str, 'whatever');
春花秋月 2024-11-16 06:08:27

首先,foreach 不是必需的。其次,如果需要,我们可以非常简单地截断它。

// Maximum length of the string; note that this does not include the '...'
$length = 20; 

// This is PHP 5.3 only and converts the value array to an array of names 
// $value = array_map(function ($f) { return $f["name"];}, $value);

// This is a PHP 5.2 way to do the array mapping
$value = array_map(create_function('$f', 'return $f["name"];'), $value);

$string = join(', ', $value);
$truncated = (strlen($string) > $length) 
    ? substr($string, 0, $length) . '...' 
    : $string;

这还有一个好处,就是不会留下难看的尾随逗号。

First up, the foreach is not required. Second, we can then truncate it if required quite simply.

// Maximum length of the string; note that this does not include the '...'
$length = 20; 

// This is PHP 5.3 only and converts the value array to an array of names 
// $value = array_map(function ($f) { return $f["name"];}, $value);

// This is a PHP 5.2 way to do the array mapping
$value = array_map(create_function('$f', 'return $f["name"];'), $value);

$string = join(', ', $value);
$truncated = (strlen($string) > $length) 
    ? substr($string, 0, $length) . '...' 
    : $string;

This also has the benefit of not leaving an ugly trailing comma.

烟雨扶苏 2024-11-16 06:08:27
function truncate_text($text, $nbrChar, $append='...') {
     if(strlen($text) > $nbrChar) {
          $text = substr($text, 0, $nbrChar);
          $text .= $append;
     }
     return $text;
}

然后在 foreach 中你可以这样做,

foreach ($value as $row) {
     $name = truncate_text($row['name']);
}

现在 $name 将输出截断的文本。

还可以考虑在循环或条件中使用方括号,虽然这不是必需的,但它在调试代码时会对您有很大帮助,因为使用方括号 {} 可以正确缩进代码。

function truncate_text($text, $nbrChar, $append='...') {
     if(strlen($text) > $nbrChar) {
          $text = substr($text, 0, $nbrChar);
          $text .= $append;
     }
     return $text;
}

and then in the foreach you can do it like this

foreach ($value as $row) {
     $name = truncate_text($row['name']);
}

now $name will output truncated text.

also consider using brackets for loops, or conditions, although it is not necessary but it will help you a lot while debugging the code as using brackets {} can indent your code properly.

桃酥萝莉 2024-11-16 06:08:27

您可以使用 break 来完成此操作。这样做

$limit = 20;
$str = '';
foreach ($value as $row) {
   if ($str > $limit) {
      $str .= substr($row, 0, $limit) . '...';
      break;
   }
   $str .= "$row, ";
   $limit -= strlen($row);
}
$str = rtrim($str, ', ');

的(次要)优点是您不必遍历整行。

You can do this with break

$limit = 20;
$str = '';
foreach ($value as $row) {
   if ($str > $limit) {
      $str .= substr($row, 0, $limit) . '...';
      break;
   }
   $str .= "$row, ";
   $limit -= strlen($row);
}
$str = rtrim($str, ', ');

The (minor) advantage of this is that you don't have to iterate through the entire row.

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