1

我想更改标记本身,在 QueryPath 中标识。具体来说,我想将这样的锚标记转换
<a href="abc.html">Example</a>

<?php Blah-Blah ?>Example</a>

<?php Blah-Blah2 ?>

我可以找到锚标记并检索其元素:
$qp->find('a[href]'); $href = $qp->attr('href');
但是,有没有办法在 QueryPath 中更改/替换标记本身?

或者,我可以用<div id="specific">标签包装组件 - 如果可能的话,我想我可以用 搜索它$qp->top('div[id="specific"]');,然后可以用 php 代码替换整个子项(锚标记及其元素)。

但是,我在 QueryPath 中找不到任何一种方式......

4

1 回答 1

4
function renameTag( DOMElement $oldTag, $newTagName ) {
   $document = $oldTag->ownerDocument;

   $newTag = $document->createElement($newTagName);
   $oldTag->parentNode->replaceChild($newTag, $oldTag);

   foreach ($oldTag->attributes as $attribute) {
       $newTag->setAttribute($attribute->name, $attribute->value);
   }
   foreach (iterator_to_array($oldTag->childNodes) as $child) {
       $newTag->appendChild($oldTag->removeChild($child));
   }
   return $newTag;
}

$qp->find('a[href]')->each(function($index,$element){
   renameTag($element,'?php');
});

您必须迭代查询中的所有元素...

于 2014-11-05T13:25:21.763 回答