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.

6
  • Do you have this in a string ? Is that in your current PHP file ? Commented Dec 14, 2012 at 16:29
  • what have u tried so far!!! can u show ur code Commented Dec 14, 2012 at 16:30
  • @soul I don't have any Idea what should I try Commented Dec 14, 2012 at 16:31
  • Depends on actual scenario. You can preg_match too. Commented Dec 14, 2012 at 16:33
  • You realise you'll get an error here because of unescaped quotation marks (") here right? Commented Dec 14, 2012 at 16:34

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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");
});

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.