Jump to content

Some problem with hyperlink extraction


ronverdonk

Recommended Posts

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

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....
[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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.