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

php中DOMElement操作xml文档案例演示

发布时间:2022-07-08 18:23:20 所属栏目:PHP教程 来源:互联网
导读:复制代码 代码如下:www.CuoXIn.com ?php //Store your html into $html variable. $html=html head titleRakesh Verma/title /head body a href=http://example.comExample/a a href=http://google.comGoogle/a a href=http://www.yahoo.comYahoo/a /body
  复制代码 代码如下:www.CuoXIn.com
 
  <?php
 
  //Store your html into $html variable.
 
  $html="<html>
 
  <head>
 
  <title>Rakesh Verma</title>
 
  </head>
 
  <body>
 
  <a href='http://example.com'>Example</a>
 
  <a href='http://google.com'>Google</a>
 
  <a href='http://www.yahoo.com'>Yahoo</a>
 
  </body>
 
  </html>";
 
  $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;
 
  ?>。
 
 

(编辑:PHP编程网 - 钦州站长网)

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