PHP - 将字符串切片到一定数量的字符以下

发布于 2024-12-31 23:44:47 字数 532 浏览 0 评论 0原文

可能的重复:
如何从字符串中获取前 x 个字符,而不切断最后一个单词?
php 修剪字符串

我的问题是我需要将字符串削减到少于 30 个字符,同时还要确保字符串以单词结尾。

我一直在使用这个:

$slidedescription = substr($slidedescription,0,30).'...';

问题是它可以切入字符串中间的单词。有没有什么简单的方法可以确保它完成一个单词但长度不超过 30 个字符?

Possible Duplicate:
How to get first x chars from a string, without cutting off the last word?
php trim a string

My issue is that i need cut a string down to less than 30 characters whilst also making sure that the string finishes on a word.

I have been using this:

$slidedescription = substr($slidedescription,0,30).'...';

The problem is that it can cut into the string mid word. Is there any simple way to make sure that it finished on a word but it less than 30 characters long?

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

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

发布评论

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

评论(2

╰◇生如夏花灿烂 2025-01-07 23:44:47

可能会有很多意想不到的情况,我试图尽可能多地涵盖它们:

<?

    $string_1  = "anavaragelongword shortword";
    $string_2 = "averylongwordwhichisprobablymorethan30characters word1";
    $string_3 = "word2 word3 averylongwordwhichisprobablymorethan30characters";
    $string_4 = "three avarege words";

    $char_length = 30;
    echo slidedescription($string_1, $char_length);
    echo slidedescription($string_2, $char_length);
    echo slidedescription($string_3, $char_length);
    echo slidedescription($string_4, $char_length);

    function slidedescription($string, $char_length)
    {   
    $total_length = null;
    $slidedescription = null;

    $length = strlen($string);
    if($length>$char_length) { 

    $array = explode(" ", $string);
    foreach ($array as $key => $value) {
    $value_length[$key] = strlen($value);
    $total_length = $total_length + $value_length[$key];
    if ($total_length<=$char_length) {
    $slidedescription .= $value." "; 
    }
    if (!$slidedescription) {
    $slidedescription = substr($value,0,$char_length).'...';
    }
    }
    } else {
    $slidedescription = $string; 
    }

    $last = trim($slidedescription).'... <br />';
    return $last;
    }

There can be many unexpected situations i tried to cover as many as i can them up:

<?

    $string_1  = "anavaragelongword shortword";
    $string_2 = "averylongwordwhichisprobablymorethan30characters word1";
    $string_3 = "word2 word3 averylongwordwhichisprobablymorethan30characters";
    $string_4 = "three avarege words";

    $char_length = 30;
    echo slidedescription($string_1, $char_length);
    echo slidedescription($string_2, $char_length);
    echo slidedescription($string_3, $char_length);
    echo slidedescription($string_4, $char_length);

    function slidedescription($string, $char_length)
    {   
    $total_length = null;
    $slidedescription = null;

    $length = strlen($string);
    if($length>$char_length) { 

    $array = explode(" ", $string);
    foreach ($array as $key => $value) {
    $value_length[$key] = strlen($value);
    $total_length = $total_length + $value_length[$key];
    if ($total_length<=$char_length) {
    $slidedescription .= $value." "; 
    }
    if (!$slidedescription) {
    $slidedescription = substr($value,0,$char_length).'...';
    }
    }
    } else {
    $slidedescription = $string; 
    }

    $last = trim($slidedescription).'... <br />';
    return $last;
    }
跨年 2025-01-07 23:44:47

查找出现空格的最后一个位置

尝试使用strrpos

您可以 就像这样:

$spacelocation = strrpos($slidedescription, " ");

if($spacelocation != false)
{
    if($spacelocation < 30)
    {
        $slidedescription = substr($slidedescription,0,$spacelocation).'...';
    }
}
else
{
    $slidedescription = substr($slidedescription,0,30).'...';
}

you could try looking for the last position where a space occurs using

strrpos

you could use it like so :

$spacelocation = strrpos($slidedescription, " ");

if($spacelocation != false)
{
    if($spacelocation < 30)
    {
        $slidedescription = substr($slidedescription,0,$spacelocation).'...';
    }
}
else
{
    $slidedescription = substr($slidedescription,0,30).'...';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文