Welcome to WebmasterWorld Guest from 54.159.246.164

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg_match / ereg question: Find only non clickable urls

   
12:30 pm on Jun 6, 2005 (gmt 0)

5+ Year Member



Hi Everyone,

I'm trying to convert non clickable urls in a string into clickable urls (and add target="_blank" to the links). The preg_match / ereg pattern should ignore any urls that are already clickable (ie: the ones with <a href="..">..</a> tags).

Here's what I've come up with so far:
-------------------------
$linkstarget = "_blank";
$text = 'A clickable link: <a href="http://www.domain.com/test/test.html">clickable</a> and a non clickable link: [domain.com';...]

$replace = "<a href=\"\\2\" target=\"".$linkstarget."\">\\3</a>";
$text = eregi_replace('(<a [^<]*href=["\']?([^ "\']*)["\']?[^>]*>([^<]*)</a>)',$replace, $text);

echo $text;
-----------------------
It adds the target="_blank" part to the link that was already clickable, but now I need something that finds the non clickable link ONLY and adds the <a href="..">..</a> tags. It's very important that it will only find the non clickable url, otherwise it will also add href tags to the already clickable url.

Any help would be greatly appreciated!

Leon

1:49 pm on Jun 9, 2005 (gmt 0)

5+ Year Member



*bump*

Anyone with a possible solution?

5:42 pm on Jun 9, 2005 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcome to WebmasterWorld, GryphonLeon.

I think a look behind assertion [php.net] may be what you are wanting. I don't have time to throw down a regex right now, but the PHP manual pages might get you started in the right direction.

Give it your best effort and we'll have a look if you get stuck!