Forum Moderators: coopster

Message Too Old, No Replies

Regular Expressions Problem

         

rfontaine

6:45 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Hi,

I have a bunch of content where I want to change a certain pattern into HTML. For example, sprinkled throughout the content are several of these, with the "SOME TEXT" being different each time:

yyySOME TEXTyyy becomes
<h2>SOME TEXT</h2>

Anyone have an idea of how to do this in PHP?

Thank you in advance...

charlier

6:58 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



$newtext = preg_replace("/(SOME TEXT)/"."<h1>$1</h1>",$oldtext);

You can also make the match, replacement and $oldtext arrays and do a bunch at once.

See
http ://www.php.net/manual/en/function.preg-replace.php

rfontaine

7:08 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Charlier, that is very good. However, the SOME TEXT part is variable - it may be any text and also may occur several times throughout the page. I would like to change this:

blah blah blah yyyTHIS UNKNOWN TEXTyyy blah blah blah blah yyyTHAT UNKNOWN TEXTyyy blah blah blah blah ...

to:
blah blah blah <h2>THIS UNKNOWN TEXT</h2> blah blah blah blah <h2>THAT UNKNOWN TEXT</h2> blah blah blah blah ...

coopster

7:16 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$newtext = preg_replace("/yyy(.*)yyy/Us", "<h2>$1</h2>", $oldtext);