加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php DomXML扩展使用示例

发布时间:2020-05-25 04:45:22 所属栏目:PHP 来源:互联网
导读:php DomXML扩展使用示例

php使用DomXML扩展的方法,感兴趣的小伙伴,下面一起跟随脚本之家 jb51.cc的小编来看看吧。
经测试代码如下:


/**
 * 使用DomXML扩展的方法
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
$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>';


/***   来自脚本之家 jb51.cc(jb51.cc)   ***/

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读