1

This is the code i have, and i want to get the href value for that id

function file_get_contents_curl($url){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
}

    $str = 'someLink';
    $html = file_get_contents_curl($str);
        $doc = new DOMDocument();
        libxml_use_internal_errors(true);
        $doc->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . $html);
        libxml_clear_errors();

        $nodes = $doc->getElementsByTagName('title');
        $hrefValue = $doc->getElementById('linker'); 

someLink html example:

<html>

<head>
<title></title>
</head>

<body>

<div>one</div>

<div>two</div>

<div>
<a href="link" id="linker" />LINK HERE</a>
</div>

</body>

</html>

I want to get the href value for the id="linker".I try with getElementById() but it dosent return anything

Marian Petrov
  • 625
  • 2
  • 9
  • 21

2 Answers2

2
$nodes = $doc->getElementsByTagName('title')->item(0)->nodeValue;
        $hrefValue = $doc->getElementById('linker')->getAttribute("href"); 
  • if i change it to $hrefValue = $doc->getElementById('linker')->nodeValue; it return LINK HERE i dont to get the text for the link – Marian Petrov Jul 10 '12 at 07:35
-1
preg_match('/href=[\'"]?(.+?)[\'"]?.*?id=[\'"]?linker[\'"]?/si', $subject, $matches);
var_dump($matches);
pamil
  • 980
  • 2
  • 10
  • 20
  • "[Regex-based HTML parsers are the cancer that is killing StackOverflow](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)" – feeela Jul 10 '12 at 08:03