Welcome to WebmasterWorld Guest from

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)

New User

10+ Year Member

joined:June 5, 2005
votes: 0

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!


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

New User

10+ Year Member

joined:June 5, 2005
votes: 0


Anyone with a possible solution?

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


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

joined:July 31, 2003
votes: 0

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!


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members