Jump to content


Photo

Some problem with hyperlink extraction


  • Please log in to reply
4 replies to this topic

#1 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 22 August 2006 - 10:48 PM

I am a real newbee at this, but I use the following expression (from this forum) to construct a hyperlink from a text file.

$html = preg_replace("/[^\"'=]((http|ftp|https):\/\/[^\s\"']+)/i", "<a href=\"\\1\">\\1</a>", $html);

When the $html input line contains:
Track follow-ups to this story online at http://www.mysite.com/.

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 athttp://www.mysite.com/.

I just can't get it right. Please help!



RTFM is an almost extinct art form, it should be subsidized.

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 22 August 2006 - 11:03 PM

Whatever [^\"'=] is finding--in this case a space--it is not being put back. To avoid changing your backreferences, use a lookbehind: (?<=[^\"'=]). You could also use (?<![\"'=]). I'm not sure which, if either, is more efficient. Hmmm....
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 22 August 2006 - 11:18 PM

  $html = preg_replace("/((http|ftp|https):\/\/[^\s\"']+\b)\//i", "<a href=\"\\1\">\\1</a>", $html);

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
What is the CoronaMatrix?

#4 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 26 August 2006 - 12:24 PM

Thanks corona!

Ronald  :cool:
RTFM is an almost extinct art form, it should be subsidized.

#5 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 27 August 2006 - 01:59 AM

No problem :)... glad to help.
What is the CoronaMatrix?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users