<div class="codetitle"><a style="CURSOR: pointer" data="33730" class="copybut" id="copybut33730" onclick="doCopy('code33730')"> 代码如下:<div class="codebody" id="code33730"> <?php //Store your html into $html variable. $html=" Rakesh Verma Example Google Yahoo "; $dom = new DOMDocument(); $dom->loadHTML($html); //Evaluate Anchor tag in HTML $xpath = new DOMXPath($dom); $hrefs = $xpath->evaluate("/html/body//a"); for ($i = 0; $i < $hrefs->length; $i++) { $href = $hrefs->item($i); $url = $href->getAttribute('href'); //remove and set target attribute $href->removeAttribute('target'); $href->setAttribute("target","_blank"); $newURL=$url.".au"; //remove and set href attribute $href->removeAttribute('href'); $href->setAttribute("href",$newURL); } // save html $html=$dom->saveHTML(); echo $html; ?> 例2 <div class="codetitle"><a style="CURSOR: pointer" data="25444" class="copybut" id="copybut25444" onclick="doCopy('code25444')"> 代码如下:<div class="codebody" id="code25444"> /<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml-stylesheet type="text/css" href="css.css"?> <班级> <学生 number="101"> <名字>孙悟空</名字> <名字>孙行者</名字> <年龄>123</年龄> <介绍><![CDATA[&$%特殊字串^&#$&]]></介绍> </学生> <学生 number="10"2"> <名字>白骨精</名字> <年龄>140</年龄> <介绍>介绍内容</介绍> </学生> </班级> */ $xmldoc = new DOMDocument('1.0','UTF-8'); $xmldoc->load('datas.xml'); $itemsNodeList = $xmldoc->getElementsbyTagName('学生'); $itemElement = $itemsNodeList->item(0);//得到第一个完整的学生信息节点 $itemChildsNodeList = $itemElement->getElementsbyTagName('名字');//得到子节点“名字”,也许有多个名字 $itemChildNode = $itemChildsNodeList->item(0);//得到第一个名字节点 echo $itemChildNode->nodeValue;//输出节点值 //封装成函数 $nodeArr = array('名字','年龄','介绍'); function getNodeVal($xmldoc,$itemsName,$nodeArr){ $items = $xmldoc->getElementsByTagName($itemsName); for($i=0; $i < $items->length; $i++){ $item = $items->item($i); foreach($nodeArr as $node){ $data[$i][] = $item->getElementsByTagName($node)->item(0)->nodeValue; } } return $data; } $data = getNodeVal($xmldoc,'学生',$nodeArr); print_r($data);
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|