returning first paragraph from a string
| From: | Lowell Allen | Date: | Thu, 04 Jul 2002 16:41:10 +0000 |
| Subject: | returning first paragraph from a string | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-105469@lists.php.net to get a copy of this message | ||
I'm having difficulty returning the first paragraph of a string called
$summary. (Usually this string will only be one paragraph, but it might
contain more than one paragraph. Current data has one occurrence of a
two-paragraph $summary.)
Here's how I process when I want multiple paragraph display. This
successfully generates the necessary HTML:
$summary = trim($summary);
$summary = ereg_replace("\r", "", $summary);
$summary = ereg_replace("\n\n", "</p><p>", $summary);
$summary = ereg_replace("\n", "<br/>", $summary);
Here's what I thought would work to give me the first paragraph only:
$summary = trim($summary);
$summary = ereg_replace("\r", "", $summary);
$summary = preg_replace("/^(.*)\n/", "\1", $summary);
But for occurrences of $summary with two paragraphs, the above returns the
second paragraph (without the first).
The only way I can return the first paragraph for a two paragraph $summary
is with:
$summary = trim($summary);
$summary = ereg_replace("\r", "", $summary);
$summary = preg_replace("/(.*)$/", "\1", $summary);
This gives me the first paragraph as desired (although I don't understand
why!), but returns nothing for occurrences of $summary having only one
paragraph.
I also tried this:
$summary = trim($summary);
$summary = ereg_replace("\r", "", $summary);
$summary = ereg_replace("\n\n", "</p><p>", $summary);
$summary = preg_replace("#(.*)</p><p>#", "\1", $summary);
This also returns only the second paragraph. Can anyone point out what I'm
doing wrong, or suggest another approach?
TIA
--
Lowell Allen