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! Link to comment https://forums.phpfreaks.com/topic/18350-some-problem-with-hyperlink-extraction/ 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.... Link to comment https://forums.phpfreaks.com/topic/18350-some-problem-with-hyperlink-extraction/#findComment-78898 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 Link to comment https://forums.phpfreaks.com/topic/18350-some-problem-with-hyperlink-extraction/#findComment-78905 Share on other sites More sharing options...
ronverdonk Posted August 26, 2006 Author Share Posted August 26, 2006 Thanks corona!Ronald :cool: Link to comment https://forums.phpfreaks.com/topic/18350-some-problem-with-hyperlink-extraction/#findComment-80685 Share on other sites More sharing options...
Corona4456 Posted August 27, 2006 Share Posted August 27, 2006 No problem :)... glad to help. Link to comment https://forums.phpfreaks.com/topic/18350-some-problem-with-hyperlink-extraction/#findComment-80986 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.