doulianxi0587 2012-07-10 07:26
浏览 126
已采纳

获取具有特定ID的标记href值

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

  • 写回答

2条回答 默认 最新

  • dongzhi6905 2012-07-10 07:29
    关注
    $nodes = $doc->getElementsByTagName('title')->item(0)->nodeValue;
            $hrefValue = $doc->getElementById('linker')->getAttribute("href"); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效