Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP: Regex (Preg_match_all) To get all links

Posted on 2011-03-17
2
Medium Priority
?
473 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 75

Expert Comment

by:käµfm³d 👽
ID: 35160106
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 75

Accepted Solution

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

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 18 hours left to enroll

810 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