[ create a new paste ] login | about

Link: http://codepad.org/z1JRl8JM    [ raw code | output | fork ]

PHP, pasted on Mar 14:
<?php

$html = "
<div class='latest-news-block'>
<h2><div id='replce_head'>some text </div></h2>
<p><img src='images/news-1.jpg' align='left' /><div id='replace_body'>some  content</div></p>
 
<div class='tweets'><img src='images/tweets.jpg' /></div>
 
";

$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->validateOnParse = false;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$query = '//div[@id="replce_head"]';

$entries = $xpath->query($query);
$newelement = $doc->createTextNode('Some new node!'); 
$oldElement = $entries->item(0);
$oldElement->parentNode->replaceChild($newelement, $oldElement); 

echo $doc->saveHTML();


Output:
1
2
3
4
5
6
7
8
9
10

Warning: DOMDocument::loadHTML(): Unexpected end tag : p in Entity, line: 4 on line 15
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div class="latest-news-block">
<h2>Some new node!</h2>
<p><img src="images/news-1.jpg" align="left"></p>
<div id="replace_body">some  content</div>
 
<div class="tweets"><img src="images/tweets.jpg"></div>
</div></body></html>


Create a new paste based on this one


Comments: