ronverdonk Posted August 22, 2006 Share Posted August 22, 2006 I am a real newbee at this, but I use the following expression (from this forum) to construct a hyperlink from a text file.[code]$html = preg_replace("/[^\"'=]((http|ftp|https):\/\/[^\s\"']+)/i", "<a href=\"\\1\">\\1</a>", $html);[/code]When the $html input line contains: [code]Track follow-ups to this story online at http://www.mysite.com/. [/code]the result is that the (url) preceeding blank is gone and that the line-closing dot is in the link, so my output is:Track follow-ups to this story online at[url=http://www.mysite.com/.]http://www.mysite.com/.[/url] I just can't get it right. Please help! Quote Link to comment Share on other sites More sharing options...
effigy Posted August 22, 2006 Share Posted August 22, 2006 Whatever [tt][^\"'=][/tt] is finding--in this case a space--it is not being put back. To avoid changing your backreferences, use a lookbehind: [tt](?<=[^\"'=])[/tt]. You could also use [tt](?<![\"'=])[/tt]. I'm not sure which, if either, is more efficient. Hmmm.... Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted August 22, 2006 Share Posted August 22, 2006 [code] $html = preg_replace("/((http|ftp|https):\/\/[^\s\"']+\b)\//i", "<a href=\"\\1\">\\1</a>", $html);[/code]Seems to work although there may be some problems with it in certain situations... you may have to capture what's in front of it to make sure that it's not part of an '<a href' tag Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 26, 2006 Author Share Posted August 26, 2006 Thanks corona!Ronald :cool: Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted August 27, 2006 Share Posted August 27, 2006 No problem :)... glad to help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.