Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP: Regex (Preg_match_all) To get all links

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
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

650 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