Re: Adding text before last paragraph
| From: | Dotan Cohen | Date: | Mon, 27 Aug 2007 22:45:21 +0000 |
| Subject: | Re: Adding text before last paragraph | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-261385@lists.php.net to get a copy of this message | ||
On 27/08/07, Brian Rue <brianrue@gmail.com> wrote:
> Dotan, try this:
>
> $text="<p>First paragraph</p>\n<p>More text</p>\n<p>Some
> more
> text</p>\n<p>End of story</p>";
>
> $story = preg_replace('{(?=<p(?:>|\s)(?!.*<p(?:>|\s)))}is',
> "<p>new
> paragraph goes here</p>\n", $text);
>
> This matches a position that has an opening <p> tag (with or without
> parameters), which is NOT followed anywhere in $text by another opening <p>
> tag. The replacement string will be inserted at the matched position, which
> will be directly before the last <p> tag. Not sure if this is the most
> efficient regex, but it should get the job done. Let me know how it goes...
> I'd also be interested to hear any comments on that regex's efficiency.
>
> -Brian Rue
>
Thank you Brian. This most certainly works. I'm having a very hard
time decyphering your regex, as I'd like to learn from it. I'm going
over PCRE again, but I think that I may hit google soon. Thank you
very, very much for the working code. As usual, I have another night
of regex waiting for me...
Dotan Cohen
http://lyricslist.com/
http://what-is-what.com/