0

I have been trying to extract link with specific URL using regex but failed I tried using the below regex to extract links using PHP.

preg_match_all('/\\<a href="(.*?)\\">/', $data1, $matches);

and the HTML is here just a snippet

<a href="https://www.website.com/n/?confirm.php" ></a>

Whole html contains a lot of links I need this link.

  • Take a look at this: [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) and this: [The DOMDocument class](http://php.net/manual/de/class.domdocument.php) – insertusernamehere Sep 11 '17 at 16:01
  • Extract all URLs (preferred method is DOM), then try a `preg_grep` to output ones including the *specific part*. – revo Sep 11 '17 at 16:01
  • @revo any answer according to your way? – Owais Iqbal Sep 11 '17 at 16:03
  • What do you really want to achieve, can you extend your question? Do you want to get properties of an anchor tag with specific URL? – siniradam Sep 11 '17 at 16:15
  • @siniradam I actually only want link of the anchor tag – Owais Iqbal Sep 11 '17 at 16:16
  • Which part is specific? Try this search to see if your questions asked previously. I'm not sure that if I understand you. https://www.google.com/search?q=regex+matching+anchor+tags&oq=regex+matching+anchor+tags – siniradam Sep 11 '17 at 16:28

1 Answers1

0

This will work if I don't misunderstood your question.

$html = '<a href="https://www.website.com/n/?confirm.php" ></a>';
preg_match_all('/href="([^\s"]+)/', $html, $match);
print '<pre>';
print_r($match);
print '</pre>';
print $match[1][0];

Edited: As per comment, you didn't provided us the specific url that's why I just post a generic answer to capture href. See my below answer now. Used regex will be found here https://regex101.com/r/pnfz7E/1

$re = '/<a href="([^"]*?\/n\/\?confirm\.php)">.*?<\/a>/m';
$str = '<a href="https://www.website.com/n/?noconfirm.php">SSD</a>
<div>How are you</div>
<a href="https://www.website.com/n/?confirm.php">HDD</a>
<h2>Being Sunny</h2>
<a href="https://www.ltmgtfu.com/n/?noconfirm.php">MSD</a>
<div>How are you</div>
<a href="https://www.website.com/n/?confirm.php"></a>
<h2>Being Sunny</h2>
<a href="https://www.google.com/n/?noconfirm.php">GSD</a>
<div>How are you</div>
<a href="https://www.website.com/n/?confirm.php">LSD</a>
<h2>Being Sunny</h2>';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
print '<pre>';
print_r($matches);
print '</pre>';
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103