communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "string handling" - download "summarize article" - open demo of "summarize article"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

string handling - summarize article was created/last modified on Thu 06 May 2004 5:53 PM

this function takes the first x words or paragraphs from a long text and displays it as teaser with a "read more" link underneath.
   
<?
   
function summarize($paragraph, $limit,$link)
{
       
$tok = strtok($paragraph, " ");
       while(
$tok)
       {
           
$text .= " $tok";
           
$words++;
           if((
$words >= $limit) && ((substr($tok, -1) == "!")||(substr($tok, -1) == ".")))
               break;
           
$tok = strtok(" ");
       }
       
$text .= ' '.$link;
       return
ltrim($text);
    }
   
// use like this
$example = ' Heres some code to extract the first part of a long paragraph, e.g. to use as a summary. Starting at the beginning of the paragraph it gets as many complete sentences as are necessary to contain $limit words. For example, with $limit at 20 it would return the first two sentences of the paragraph e reading right now (the first 20 words plus the rest of the sentence in which the limit was hit)';
  
$link = '<a href="example.php?article=xxx">read more</a>';

echo
summarize($example,5,$link);
   
?>

learn more about some of the PHP functions used in this snippet: break, echo, extract, function, link, ltrim, ord, return, strtok, substr, trim, while

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari