homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

preg_match / ereg question: Find only non clickable urls

 12:30 pm on Jun 6, 2005 (gmt 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 Jun 9, 2005 (gmt 0)


Anyone with a possible solution?


 5:42 pm on Jun 9, 2005 (gmt 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!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved