Short Text, PHP

后端 未结 4 461
我寻月下人不归
我寻月下人不归 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 04:59

    Im assuming you just want to take an input. If it is longer than X then cut it off at X and add "...".

    // Start function
    function shorter($text, $chars_limit)
    {
        // Check if length is larger than the character limit
        if (strlen($text) > $chars_limit)
        {
            // If so, cut the string at the character limit
            $new_text = substr($text, 0, $chars_limit);
            // Trim off white space
            $new_text = trim($new_text);
            // Add at end of text ...
            return $new_text . "...";
        }
        // If not just return the text as is
        else
        {
        return $text;
        }
    }
    

    I didn't test this, but it should work. :)

提交回复
热议问题