Copy Code code as follows:
<?php
Store your HTML into $html variable.
$html = "
<title>rakesh verma</title>
<body>
<a href= ' http://example.com ' >Example</a>
<a href= ' http://google.com ' >Google</a>
<a href= ' http://www.yahoo.com ' >Yahoo</a>
</body>
$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;
?>
Example 2
Copy Code code as follows:
/*<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?>
<!--CSS style definition, no dot. such as: name{color:red;}-->
<?xml-stylesheet type= "Text/css" href= "Css.css"?>
<!--introduce a DTD document definition file (root element: Class) <! DOCTYPE class SYSTEM "Class.dtd"/>-->
<!--<! DOCTYPE class [
<! ELEMENT Class (student +) >
<! ELEMENT Student (name, age, Introduction) >
<! ELEMENT name (#PCDATA) >
<! ELEMENT Age (#PCDATA) >
<! ELEMENT Introduction (#PCDATA) >
]/>-->
< class >
< students ' "number=" >
< name > Monkey King </name >
< name > Sun Xing </name >
< age >123</Age >
< introduction ><! [cdata[&*$% Special string ^&#$&]]></introduction >
</Students >
< student number= "2" >
< name > Genie </name >
< age >140</Age >
< Introduction > Introduction content </Introduction >
</Students >
</class >
*/
$xmldoc = new DOMDocument (' 1.0 ', ' UTF-8 ');
$xmldoc->load (' datas.xml ');
$itemsNodeList = $xmldoc->getelementsbytagname (' student ');
$itemElement = $itemsNodeList->item (0);//Get the first complete student information node
$itemChildsNodeList = $itemElement->getelementsbytagname (' name ');//Get child node "name", maybe have more than one name
$itemChildNode = $itemChildsNodeList->item (0);//Get First name node
echo $itemChildNode->nodevalue;//Output node value
Encapsulated into functions
$NODEARR = Array (' name ', ' age ', ' introduction ');
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, ' student ', $NODEARR);
Print_r ($data);