Solved

PHP: Regex (Preg_match_all) To get all links

Posted on 2011-03-17
2
453 Views
Last Modified: 2012-05-11
I have the following:

preg_match_all("'HREF=\"([^>]*?)\"(.*?)'is",
             $messageBody,
               $linkHrefs, PREG_PATTERN_ORDER);

This works great if the link is correct (and has "" I believe). What the issue is if someone does this

<a href = "http://test.com"> <-- notice the spaces between href =

Another one could be

<a href='http://test.com'> <-- using ' instead of "

Is there any way to modify my regex so that it handles this alot more and gets MOST, if not all links, regardless of 100% perfection?

Thank you!
0
Comment
Question by:Valleriani
  • 2
2 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I would suggest using a delimiter other than single quote, but it is of course not mandatory  = )
preg_match_all("'HREF\s*=\s*\"([^>]*?)\"(.*?)'is", 
             $messageBody, 
               $linkHrefs, PREG_PATTERN_ORDER);

Open in new window

0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Correction:
preg_match_all("'HREF\s*=\s*[\"\']([^>]*?)[\"\'](.*?)'is", 
             $messageBody, 
               $linkHrefs, PREG_PATTERN_ORDER);

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now