dqp4933 2014-03-31 13:45
浏览 40
已采纳

修剪一个字符串并添加到它的末尾[重复]

This question already has an answer here:

what i am trying to do is basically shorten down a peice of text and add "..." to the end if the text is too long. However (here is where it gets hairy) i only want to apply this, if the last character of the string was a SPACE. If it is NOT space, then i want to go through the string (Backwords) until i find a space, THEN when a space is found i will add "..." to it.

The reasoning to this is i dont want to add "..." to a word when its not finished, such as "wor..." i would rather it be "word...".

Here is what i have tried so far (NOTE: $row_object_title is just a string coming from a database, so its safe to assume that its just a random text):

if (strlen($row_object_title) > 50){
    // check for spaces
        $short = substr($row_object_title,50);

        $chr = substr($short,-1);
        $ascii = ord($chr);
        if ($ascii == "32"){
            $row_object_title .= "...";
        }else{  
            $x = 49;
            while($ascii !== "32"){
                $chr = substr($short,$x);
                $ascii = ord($chr);
                if ($ascii == "32"){
                    // we got a space!
                    $row_object_title .= "...";
                }
                $x = $x - 1;                        
            }

        }
}

Thanks for the help

UPDATE:

if (strlen($row_object_title) > 50){
    // check for spaces
        $short = substr($row_object_title,50);

        $chr = substr($short,-1);
        $ascii = ord($chr);
        if ($ascii == "32"){
            //$trim = "...";
            //$final = str_replace($short,$trim,$row_object_title);
            $row_object_title .= "...";
        }else{  
            $x = 49;
            while($ascii != "32"){
                $chr = substr($short,$x,1);
                $ascii = ord($chr);
                if ($ascii == "32"){
                    // we got a space!
                    $row_object_title .= "...";
                }
                $x = $x - 1;                        
            }

        }
}

SECOND UPDATE: Every one is posting their version of the script (thanks alot) but could you guys possibly try to fix mine instead of posting other ways of doing it?

</div>
  • 写回答

4条回答 默认 最新

  • duanqin9631 2014-03-31 14:01
    关注
    $text = "This should just fit in! But if we make it longer, it won't ";
    if (strlen($text) > 50) {
        $text = substr($text, 0, 50);
        for ($i = strlen($text) - 1; $i > 0; $i--) {
            if ($text[$i] === ' ' || $text[$i] === ',' || $text[$i] === '.' || $text[$i] === '!' || $text[$i] === '?') {
                $text = substr($text, 0, $i) . '... <b>Read More</b>';
                break;
            }
        }
    }
    

    Does the job for me (should add RegEx, but I'm not that great with those...).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程