php DomXML扩展使用示例

内容摘要
这篇文章主要为大家详细介绍了php DomXML扩展使用示例,具有一定的参考价值,可以用来参考一下。

php使用DomXML扩展的方法,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代
文章正文

这篇文章主要为大家详细介绍了php DomXML扩展使用示例,具有一定的参考价值,可以用来参考一下。

php使用DomXML扩展的方法,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<code class="php">
/**
 * 使用DomXML扩展的方法
 *
 * @param
 * @arrange (www.idcnote.com)
 **/
$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>';
 
 
/***   来自php教程(www.idcnote.com)   ***/</code>

注:关于php DomXML扩展使用示例的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!