3

我想用 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#Replaceing
http://api.jquery.com/replaceWith/

这是在代码中有和没有返回的 html ->replaceWith('<p></p>')

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

5 回答 5

3

如果您不介意一个普通的 DOMDocument 解决方案(DOMDocument 在 phpQuery 的引擎盖下用于解析 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 中进行替换的正确方向?


编辑:

我给了 replaceWith 的 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;

我找不到任何方法可以通过一行中的链接方法调用来做到这一点。

于 2010-08-29T18:02:29.203 回答
1

尝试;

$formatted_event = $event->find('span')->replaceWith('<p></p>');
于 2010-08-29T07:49:10.933 回答
1

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

$htmlContent = str_replace("<span", "<p", $htmlContent);
$htmlContent = str_replace("</span>", "</p>", $htmlContent);    
于 2010-08-29T09:55:21.677 回答
0

你试过了吗:

 $formatted_event = $event->find('span')->replaceWith('p');
于 2010-08-29T08:03:30.677 回答
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;
于 2011-10-20T12:20:19.577 回答