Lucas
Lucas

Reputation: 3715

Short Text, PHP

I got this function:

function shorter($text, $chars_limit) {
  if (strlen($text) > $chars_limit) 
    return substr($text, 0, strrpos(substr($text, 0, $chars_limit), " ")).'...';
  else return $text;
}

and if I use echo shorter($input, 11) it works ok, but if there are some white spaces in the input, otherwise for the input looking like:

wwwwwwwwwww

The function will change this into:

... (3 dots).

I wan't it to be changed into something like this:

www ...

Have you got any ideas how to rebuild this script? Thank you in advance.

Upvotes: 4

Views: 15826

Answers (4)

Adam
Adam

Reputation: 723

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. :)

Upvotes: 12

cypher
cypher

Reputation: 6992

If you are looking for a function that trims some actual text, you will probably need a UTF-8 safe function. Also, if you want to trim the text somewhat intelligently (trim the text after alphanumeric characters only, no HTML and so on) you can try this funciton I wrote:

/**
 * shortens the supplied text after last word
 * @param string $string
 * @param int $max_length
 * @param string $end_substitute text to append, for example "..."
 * @param boolean $html_linebreaks if LF entities should be converted to <br />
 * @return string
 */
function mb_word_wrap($string, $max_length, $end_substitute = null, $html_linebreaks = true) { 

    if($html_linebreaks) $string = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
    $string = strip_tags($string); //gets rid of the HTML

    if(empty($string) || mb_strlen($string) <= $max_length) {
        if($html_linebreaks) $string = nl2br($string);
        return $string;
    }

    if($end_substitute) $max_length -= mb_strlen($end_substitute, 'UTF-8');

    $stack_count = 0;
    while($max_length > 0){
        $char = mb_substr($string, --$max_length, 1, 'UTF-8');
        if(preg_match('#[^\p{L}\p{N}]#iu', $char)) $stack_count++; //only alnum characters
        elseif($stack_count > 0) {
            $max_length++;
            break;
        }
    }
    $string = mb_substr($string, 0, $max_length, 'UTF-8').$end_substitute;
    if($html_linebreaks) $string = nl2br($string);

    return $string;
}

Upvotes: 5

Herbert
Herbert

Reputation: 5778

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;
}

Upvotes: 1

oezi
oezi

Reputation: 51797

assuming the behaviour for strings containing whitespace shouldn't change, try this:

function shorter($text, $chars_limit) {
  if (strlen($text) > $chars_limit) {
    $rpos = strrpos(substr($text, 0, $chars_limit), " ");
    if ($rpos!==false) {
      // if there's whitespace, cut off at last whitespace
      return substr($text, 0, $rpos).'...'; 
    }else{
      // otherwise, just cut after $chars_limit chars
      return substr($text, 0, $chars_limit).'...'; 
    }
  } else {
    return $text;
  }
}

Upvotes: 1

Related Questions