Jump to content

Find link in string using regex

- - - - -

  • Please log in to reply
2 replies to this topic

#1
liamzebedee

liamzebedee

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
I recently decided I wanted to find all the links(<a href="...">...</a>) referenced in an SQL database. I already have the code for extracting the text which contains the links, now I need some help on which function to use. I have decided I will be using a regular expression to find the links however I don't know which function I need to use.
I want to be finding the link in the string, then printing it, thats all.
Here is my current prototype for the regex string-
/<(a) href="


[(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))


]">[^A-Za-z0-9\s]*</a>


#2
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
If you add a / at the end of the regular expression too, you could use preg_match for this. Ereg(i) is an older version which can be used but isn't recommended, rather use the newer version (preg_match) which is as well faster.

btw, I don't get a clue of your regular expression haha :-P

#3
liamzebedee

liamzebedee

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
Lol I copy pasted the middle bit BTW. Someone actually wrote that!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users