Solved

PHP: Regex (Preg_match_all) To get all links

Posted on 2011-03-17
2
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 total points
ID: 35160120
Correction:
preg_match_all("'HREF\s*=\s*[\"\']([^>]*?)[\"\'](.*?)'is", 
             $messageBody, 
               $linkHrefs, PREG_PATTERN_ORDER);

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

717 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