-8

I'm working on news script and I would like to create summary function to get little of text from full text content. The function would get 200 characters of full text and then it would search for last space of these 200 characters. The function must also ignore any html or other code and show only first 200 characters of full text content.

Which function is the best for my problem and how to do that function?

user1257255
  • 1,161
  • 8
  • 26
  • 55

4 Answers4

6

Checkout this helper function from the CodeIgniter framework:

/**
* Character Limiter
*
* Limits the string based on the character count.  Preserves complete words
* so the character count may not be exactly as specified.
*
* @access   public
* @param    string
* @param    integer
* @param    string  the end character. Usually an ellipsis
* @return   string
*/

function character_limiter($str, $n = 500, $end_char = '…')
{
    if (strlen($str) < $n)
    {
        return $str;
    }

    $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));

    if (strlen($str) <= $n)
    {
        return $str;
    }

    $out = "";
    foreach (explode(' ', trim($str)) as $val)
    {
        $out .= $val.' ';

        if (strlen($out) >= $n)
        {
            $out = trim($out);
            return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
        }
    }
 }

You can use the function:

echo character_limiter($text, 200);

If you need to avoid HTML tags, you can use strip_tags function before calling the character_limiter function.

Stelian Matei
  • 11,553
  • 2
  • 25
  • 29
3

The best solution would be apparently a designated "summary" field in the database filled by the editor manually.

It will save you a lot of headaches in the future.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

I use the following function to make sure that the length of my teaser string doesn't exceed the specified maxlength, but still keeps whole words.

function teaser( $input, $length = 200)
{
  if( strlen($input) <= $length )
    return $input;

  $parts = explode(" ", $input);

  while( strlen( implode(" ", $parts) ) > $length )
    array_pop($parts);

  return implode(" ", $parts);
}

Short and simple.

Repox
  • 15,015
  • 8
  • 54
  • 79
  • And if you feel the need to put something like "..." in the end of the shortened string, just extend the return line to something like: `return implode(" ", $parts) . "...";` – Repox Apr 29 '12 at 09:40
0

There is a simple function called substr($string, $position, $length), which can do this.

Use it like

$brief = substr($bigContent, 0, 200); //for 200 chars
Starx
  • 77,474
  • 47
  • 185
  • 261
  • I believe it requires a bit more logic than a simple `substr`. The idea is to end the text after the last word that would fit in the the 200 length, otherwise the article might end in the middle of a word. – Stelian Matei Apr 29 '12 at 09:37
  • @mazzucci, Oh.. Please do not start on the how complex this could be, I already have coded a hectic module for this alone. :P – Starx Apr 29 '12 at 09:58