Forum Moderators: coopster

Message Too Old, No Replies

Add target to each link and image with regex

Only for Hard Core Regexp-ers ;)

         

adnanp78

8:43 pm on May 23, 2007 (gmt 0)

10+ Year Member



So guys, if you are one of them:

How to make regexp command in PHP there paste code target="blank" on every link (include image links to) in text (whole text is in one variable).
-------------
refine (not neccesary) if target="blank" already exist skip it.

Lets go! ;)

[edited by: eelixduppy at 8:44 pm (utc) on May 23, 2007]
[edit reason] spelling [/edit]

eelixduppy

9:12 pm on May 23, 2007 (gmt 0)




$string = 'your html here';
$pattern = "/<(aŠimg)([^>]+)>/i";
$replacement = "<\\1 target=\"_blank\"\\2>";
$new_str = preg_replace($pattern,$replacement,str_replace('target="_blank"','',$string));

Try something like that :) Refer to pattern syntax [us.php.net] for more information. Also, make sure to replace the 'Š' with the solid pipe character as webmasterworld breaks these.

adnanp78

1:52 pm on May 24, 2007 (gmt 0)

10+ Year Member



Tnx very much ;) you are the winner