Sunday, March 20, 2011

PHP Regular expression

I need to add href=" before an http:// if this http:// doesn't follow href=" or src="

Following code partly works. Partly means it considers <a href=" only but not src="

$str= preg_replace( 
    "/(?<!a href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", 
    "<a href=\"\\0\"> target=\"blank\">\\0</a>", 
    $str
);

Thank you guys in advance for your reply.

From stackoverflow
  • $str= preg_replace( 
        "/(?<!a href=\")(?<!src=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", 
        "<a href=\"\\0\"> target=\"blank\">\\0</a>", 
        $str
    );
    
    David : works well. Thank you.

0 comments:

Post a Comment