使用phpquery替换元素(jquery的php版本)

时间:2010-08-29 07:45:57

标签: php phpquery

我想使用phpquery将所有<span>标记替换为<p>。我的代码出了什么问题?它找到span但是replaceWith函数没有做任何事情。

$event = phpQuery::newDocumentHTML(file_get_contents('event.html'));
$formatted_event = $event->find('span')->replaceWith('<p>');

此文档表明这是可能的:
http://code.google.com/p/phpquery/wiki/Manipulation#Replacing
http://api.jquery.com/replaceWith/

这是在代码中使用和不使用->replaceWith('<p></p>')返回的html:

<span class="Subhead1">Event 1<br></span><span class="Subhead2">Event 2<br>
    August 12, 2010<br>
    2:35pm <br>
    Free</span>

5 个答案:

答案 0 :(得分:3)

如果你不介意一个普通的DOMDocument解决方案(DOMDocument用于解析HTML片段的问题),我前段时间做了类似的事情。我调整了代码以满足您的需求:

$document = new DOMDocument();

// load html from file
$document->loadHTMLFile('event.html');

// find all span elements in document
$spanElements = $document->getElementsByTagname('span');
$spanElementsToReplace = array();

// use temp array to store span elements
// as you cant iterate a NodeList and replace the nodes
foreach($spanElements as $spanElement) {
  $spanElementsToReplace[] = $spanElement;
}

// create a p element, append the children of the span to the p element, 
// replace span element with p element
foreach($spanElementsToReplace as $spanElement) {
    $p = $document->createElement('p');

    foreach($spanElement->childNodes as $child) {
        $p->appendChild($child->cloneNode(true));
    }

    $spanElement->parentNode->replaceChild($p, $spanElement);
} 

// print innerHTML of body element
print DOMinnerHTML($document->getElementsByTagName('body')->item(0));


// --------------------------------


// Utility function to get innerHTML of an element
// -> "stolen" from: http://www.php.net/manual/de/book.dom.php#89718
function DOMinnerHTML($element) {
    $innerHTML = "";
    $children = $element->childNodes;
    foreach ($children as $child)
    {
        $tmp_dom = new DOMDocument();
        $tmp_dom->appendChild($tmp_dom->importNode($child, true));
        $innerHTML.=trim($tmp_dom->saveHTML());
    }
    return $innerHTML;
} 

也许这可以帮助你找到如何在phpQuery中进行替换的正确方向?


编辑:

我给了另一个jQuery文档替换看起来,在我看来,你必须传入整个html片段,你想要成为新的,替换内容。

此代码剪辑为我工作:

$event = phpQuery::newDocumentHTML(...);

// iterate over the spans
foreach($event->find('span') as $span) {
// make $span a phpQuery object, otherwise its just a DOMElement object
$span = pq($span);
// fetch the innerHTMLL of the span, and replace the span with <p>
$span->replaceWith('<p>' . $span->html() . '</p>');
}

print (string) $event;

我无法通过一行中的链式方法调用找到任何方法。

答案 1 :(得分:1)

尝试;

$formatted_event = $event->find('span')->replaceWith('<p></p>');

答案 2 :(得分:1)

不会str_replace做得更好吗?它更快,更容易调试。 始终考虑到外部库可能存在错误:)

$htmlContent = str_replace("<span", "<p", $htmlContent);
$htmlContent = str_replace("</span>", "</p>", $htmlContent);    

答案 3 :(得分:0)

你有没有尝试过:

 $formatted_event = $event->find('span')->replaceWith('p');

答案 4 :(得分:0)

你实际上可以使用wrap函数

$event = phpQuery::newDocumentHTML(...);

// iterate over the spans
foreach($event->find('span') as $span) {
  // make $span a phpQuery object, otherwise its just a DOMElement object
  $span = pq($span);
  // wrap the span with <p>
  $span->wrap('<p></p>');
}

print (string) $event;