龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

DomXML范例

时间:2014-06-17 15:28来源:网络整理 作者:网络 点击:
分享到:
DomXML范例 [代码片段(47行)]
// example HTML code: (could also come from an URL)
$html = '<html>
<head>
<title>links</title>
</head>
<body>
<a href="link1.htm" title="Link title 1" target="_blank">Link #1</a><br/>
<a href="link2.htm" title="Link title 2" target="_blank">Link #2</a><br/>
<a href="link3.htm" title="Link title 3" target="_blank">Link #3</a><br/>
</body>
</html>';

// check if DomXML is available:
if (!function_exists('DomDocument')){
    die('DomXML extension is not available :-(');
}

print '<pre>';

// create new DOM object:
$dom = new DomDocument();

// load HTML code:
$dom->loadHtml($html);

// get tags by tagname (all <a> tags / links):
$tags = $dom->getElementsByTagName('a');

// loop trough all links:
foreach ($tags as $a){

    print '<b>' . $a->nodeValue . '</b><br/>';

    // does this tag have attributes:
    if ($a->hasAttributes()){    

        // loop trough all attributes:
        foreach ($a->attributes as $attribute){      
            print '- ' . $attribute->name . ': ' . $attribute->value;
            print "<br/>";                
        }
    }

    print "<hr/>";
}

print '</pre>';
收藏文章
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后...
评论内容为空!
还没有评论,快来抢沙发吧!

热评话题

按钮 内容不能为空!
立刻说两句吧! 查看0条评论
精彩图集

赞助商链接