string handling
|summarize article
| see demonstration of summarize articlethis function takes the first x words or paragraphs from a long text and displays it as teaser with a "read more" link underneath.
full source of summarize article [ line 1 - 23 ] | download summarize article
| 1 | <? |
| 2 | function summarize($paragraph, $limit,$link) |
| 3 | { |
| 4 | $tok = strtok($paragraph, " "); |
| 5 | while($tok) |
| 6 | { |
| 7 | $text .= " $tok"; |
| 8 | $words++; |
| 9 | if(($words >= $limit) && ((substr($tok, -1) == "!")||(substr($tok, -1) == "."))) |
| 10 | break; |
| 11 | $tok = strtok(" "); |
| 12 | } |
| 13 | $text .= ' '.$link; |
| 14 | return ltrim($text); |
| 15 | } |
| 16 | // use like this |
| 17 | $example = ' Heres some code to extract the first part of a long paragraph, e.g. to use as a summary. |
| 18 | Starting at the beginning of the paragraph it gets as many complete sentences as are necessary to contain $limit words. |
| 19 | For example, with $limit at 20 it would return the first two sentences of the paragraph e reading right now |
| 20 | (the first 20 words plus the rest of the sentence in which the limit was hit)'; |
| 21 | $link = '<a href="#">read more</a>'; |
| 22 | echo summarize($example,5,$link); |
| 23 | ?> |
19 hits by 13 users in the last 30 minutes.