开发者

Replace element using phpquery (php version of jquery)

开发者 https://www.devze.com 2023-01-14 13:25 出处:网络
I want to replace all <span> tags with <p> using phpquery. What is wrong with my code? It finds the span but the replaceWith function is not doing anything.

I want to replace all <span> tags with <p> using phpquery. What is wrong with my code? It finds the span but the replaceWith function is not doing anything.

$event = phpQuery::newDocumentHTML(fi开发者_StackOverflowle_get_contents('event.html'));
$formatted_event = $event->find('span')->replaceWith('<p>');

This documentation indicates this is possible:

http://code.google.com/p/phpquery/wiki/Manipulation#Replacing

http://api.jquery.com/replaceWith/

This is the html that gets returned with and without ->replaceWith('<p></p>') in the code:

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


If you dont mind a plain DOMDocument solution (DOMDocument is used unter the hood of phpQuery to parse the HTML fragments), I did something similiar a while ago. I adapted the code to do what you need:

$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;
} 

Maybe this can get you in the right direction on how to do the replacement in phpQuery?


EDIT:

I gave the jQuery documentation of replaceWith another look, it seems to me, that you have to pass in the whole html fragment which you want to be your new, replaced content.

This code snipped worked for me:

$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;

I couldnt find any way to do this with chained method calls in one line.


Wont str_replace do a better job at this? Its faster and easier to debug. Always take into consideration that external libraries may have bugs:)

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


Try;

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


Did you try:

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


You could actually use the wrap function

$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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号