Re: Adding text before last paragraph

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 27/08/07, Brian Rue <brianrue@xxxxxxxxx> 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/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux