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
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....
Link to comment
Share on other sites

[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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.