replace last occurrence of a string with another string

St_Aug_Beach_Bum
St_Aug_Beach_Bum used Ask the Experts™
on
arrgh...

Ok, I'm trying to find a way to get the last ending p tag </p> and place a string in front of it.

So:

$string = "<p>Some text</p><p>More text.<p/><p>Even more text.</p>";

$insert = " Here is an insert.";

results need to be:
---------
<p>Some text</p><p>More text.<p/><p>Even more text. Here is an insert.</p>
-----------

Help would be greatly appreciated - this has been quite a headache!

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Project Lead
Commented:
this should work..

$string = preg_replace("#(<p>.*)(</p>)#is","$1".$insert."$2",$string);

Open in new window

Author

Commented:
Thank you! Yes, working perfectly -  Chris

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial