Solved

PHP: Regex (Preg_match_all) To get all links

Posted on 2011-03-17
2
458 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

828 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