How to get link from any anchor using regular expression?

Asked

Viewed 301 times

1

I use this code to capture links from a particular page:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    
$resultado = curl_exec($ch);

preg_match_all('/<a href="/(.*)"/i', $resultado, $outros);

However, this regular expression leaves out links such as:

<a name="exemplo" href="link.php"></a>

And if I take the <a and leave the href for example:

preg_match_all('/href="/(.*)"/i', $resultado, $outros);

there will pick up improper things like css links for example:

<link href="link.css">

What is the ideal regular expression to capture all href of the elements a without having the risk of capturing href of elements that are not a, as css for example?

1 answer

2


Try that way, ignoring what’s in between a and href:

preg_match_all('/<a.*href="(.*)"/i', $resultado, $outros);

I hope I’ve helped!

  • Thank you very much friend. PERFECT!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.