xhtml:
<root> <a href="1#fn1" class="fn-ref" id="s9781473910270.i11"><sup>1</sup></a> <p>some text<a href="1#fnref1" id="s9781473910270.i237">↩</a></p> </root> from above sample, if href of both anchor same (like 1#fn1 , 1#fnref1) have exchange id's href , have wrap anchor tag(which has fnref in href in it) <span class="label-fn"> need set class="ref-fn-ref" anchor tag.
expected output:
<root> <a href="#s9781473910270.i237" class="fn-ref" id="s9781473910270.i11"><sup>1</sup> </a> <p>sometext<span class="label-fn"><a href="#s9781473910270.i11" class="ref-fn-ref" id="s9781473910270.i237">↩</a></span></p> </root> i have tried far,
libxml_use_internal_errors(true); $dom = new domdocument; $dom->loadhtmlfile("sample.xhtml", libxml_html_noimplied | libxml_html_nodefdtd); $xp = new domxpath($dom); $xp->registernamespace("php", "http://php.net/xpath"); $classname="fn-ref"; $anchor1 = $xp->query("//[contains(@class, '$classname')]"); //???? i stucked in between, don't know how array of anchor tags matched anchor1 array.
figured out myself output.
$dom = new domdocument; $dom->loadhtml($html, libxml_html_noimplied | libxml_html_nodefdtd); $xp = new domxpath($dom); $xp->registernamespace("php", "http://php.net/xpath"); $a1 = $xp->query("//*[contains(@class, 'fn-ref')]"); $a2 = $xp->query("//*[contains(@href, '#fnref')]"); foreach($a1 $a1v) { $id1=$a1v->getattribute('id'); preg_match("/([\\d]+)#fn([\\d+])/s",$a1v->getattribute('href'),$m1); $href1=$m1[1]."#fnref".$m1[2]; foreach($a2 $a2v) { if($a2v->getattribute('href')===$href1){ $id2=$a2v->getattribute('id'); $a2v->setattribute('class','ref-fn-ref'); $a2v->setattribute('href',"#".$id1); $a1v->setattribute('href',"#".$id2); break; } } } $r=$dom->savexml();
Comments
Post a Comment