php trim a string

前端 未结 4 809
灰色年华
灰色年华 2020-12-18 16:37

I\'m trying to build a function to trim a string is it\'s too long per my specifications.

Here\'s what I have:

function trim_me($s,$max) 
{
    if (s         


        
4条回答
  •  抹茶落季
    2020-12-18 17:08

    function trim_me($s,$max) {
        if( strlen($s) <= $max) return $s;
        return substr($s,0,strrpos($s," ",$max-3))."...";
    }
    

    strrpos is the function that does the magic.

提交回复
热议问题