Short Text, PHP

后端 未结 4 476
我寻月下人不归
我寻月下人不归 2021-01-03 04:51

I got this function:

function shorter($text, $chars_limit) {
  if (strlen($text) > $chars_limit) 
    return substr($text, 0, strrpos(substr($text, 0, $ch         


        
4条回答
  •  感情败类
    2021-01-03 05:13

    function shorter($input, $length)
    {
        //no need to trim, already shorter than trim length
        if (strlen($input) <= $length) {
            return $input;
        }
    
        //find last space within length
        $last_space = strrpos(substr($input, 0, $length), ' ');
        if(!$last_space) $last_space = $length;
        $trimmed_text = substr($input, 0, $last_space);
    
        //add ellipses (...)
        $trimmed_text .= '...';
    
        return $trimmed_text;
    }
    

提交回复
热议问题