How to get first x chars from a string, without cutting off the last word?

后端 未结 13 1348
再見小時候
再見小時候 2020-11-28 10:24

I have the following string in a variable.

Stack Overflow is as frictionless and painless to use as we could make it.

I want to fetch first 28 characte

13条回答
  •  醉酒成梦
    2020-11-28 10:42

    This's Working for me Perfect

    function WordLimt($Keyword,$WordLimit){
    
        if (strlen($Keyword)<=$WordLimit) { return $Keyword; }
        $Keyword= substr($Keyword,0,strrpos(substr($Keyword,0,$WordLimit),' '));
        return $Keyword;
    }
    
    echo WordLimt($MyWords,28);
    
    // OutPut : Stack Overflow is as
    

    it will adjust and break on last Space without cut word...

提交回复
热议问题