-2

Possible Duplicate:
Grabbing the href attribute of an A element

how can i find the values of all the href in a characters chain.

Example :

<a href = "blabla1"> Test1 </a>
<a href = "blabla2"> Test1 </a>

I want to retrieve blabla1 and blabla2.

I tried regular expression but I give up ! XD

Thanks

Community
  • 1
  • 1
Hulk
  • 27
  • 4
  • That's your problem: don't use regexes for DOM manipulation. It's 2013. Use an XML parser. –  Feb 04 '13 at 20:09

2 Answers2

2

You can use php DOM

$str = <<<H
<a href="blabla1">Test1</a>
<a href="blabla2">Test1</a>
H;

$dom = new DOMDocument();
$dom->loadHTML($str);

foreach($dom->getElementsByTagName("a") as $node){
    echo $node->getAttribute("href") . "<br>";
}

Here is documentation of DOM

nanobash
  • 5,419
  • 7
  • 38
  • 56
1

XPath can do the job of getting the value of the attribute with $xpath->query("//a/@value");. Then you can iterate over the node list of attribute nodes and access the $value property of each attribute node.

Henk Kroon
  • 71
  • 1
  • 6