0

Possible Duplicate:
Grabbing the href attribute of an A element

I have the following code :

<?php $a= "<a href="http://www.kqzyfj.com/click-6331466-10751223" target="_top">
<img src="http://www.tqlkg.com/image-6331466-10751223" width="300" height="250" alt="Kaspersky Anti-Virus 2013" border="0"/></a>" ?>

Please suggest me any Idea that how can I retrieve the URL (http://www.kqzyfj.com/click-6331466-10751223) in php.

Community
  • 1
  • 1
Haren Sarma
  • 2,267
  • 6
  • 43
  • 72

3 Answers3

4

If you're parsing HTML to extract href attribute values from anchor tags, use an HTML/DOM Parser (definitely don't use regex).

PHP Simple HTML DOM Parser

PHP XML DOM

OR

Don't use regexes to parse HTML. Use the PHP DOM:

$DOM = new DOMDocument;
$DOM->loadHTML($str); // Your string

//get all anchors
$anchors = $DOM->getElementsByTagName('a');

//display all hrefs
for ($i = 0; $i < $anchors->length; $i++)
    echo $anchors->item($i)->getAttribute('href') . "<br />";

You can check if the node has a href using hasAttribute() first if necessary.

and this link

Community
  • 1
  • 1
Ghostman
  • 6,042
  • 9
  • 34
  • 53
0

Try either jquery or DOMXPATH

Here's the link to DOMXPATH

Using Jquery, you can get its value by putting a selector to it, say id,

<a href="http://www.kqzyfj.com/click-6331466-10751223" target="_top" id='url'>

And, then access it as,

$(document).ready(function(){
 var link = $("#url").attr("href");
});
Community
  • 1
  • 1
Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
-2

You could use a regular expression:

preg_match("/href=\"(http:\/\/.*)\"/U",$a, $matches);

It will search for text between 'href="' and '"' $matches[1] will contain your url.

Maarten
  • 31
  • 2