@ 2017-07-13T08:20:31Z <?php
class MyParser {
private $domdoc;
public function __construct(DOMDocument $domdoc){
$this->domdoc = $domdoc;
}
public function __toString()
{
return $this->domdoc->saveHTML();
}
public function replaceVariableInside($varName, $text, $idType, $idValue)
{
$nodes = $this->getNodes($idType, $idValue);
$newTextNode = $this->getNodeFromText($text);
array_map(function($node) use ($varName, $newTextNode){
/* @var $node DOMNode */
$textNodes = array_filter($this->dnl2array($node->childNodes), function($node){
return $node->nodeType === XML_TEXT_NODE;
});
foreach ($textNodes as $textNode)
{ /* @var $textNode \DOMText */
$pos = strpos($textNode->nodeValue, $varName);
if($pos !== false)
{
$newNode = $textNode->splitText($pos);
$newNode->nodeValue = substr($newNode->nodeValue, strlen($varName));
$newNode->insertBefore($newTextNode);
}
}
}, $nodes);
}
protected function getNodes($idType, $idValue)
{
switch ($idType){
case 'id':
return [
$this->domdoc->getElementById($idValue)
];
break;
case 'class':
return $this->dnl2array((new DOMXPath($this->domdoc))
->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $idValue ')]"));
break;
default:
break;
}
}
public function insertInto($idType, $id, $text)
{
$newnode = $this->getNodeFromText($text);
array_map(function($elementOfDom) use ($newnode){
/* @var $elementOfDom DOMNode */
@$elementOfDom->appendChild($newnode);
}, $this->getNodes($idType, $id));
}
protected function getNodeFromText($text)
{
$text = html_entity_decode($text);
try
{
$tmp = new DOMDocument;
$textDocumentRes = @$tmp->loadHTML('<?xml encoding="utf-8" ?><span>'.$text.'</span>');
}
catch(Exception $e)
{
$textDocumentRes = false;
}
if($textDocumentRes)
{
$newnode = $this->domdoc->importNode($tmp->documentElement->firstChild->firstChild, true);
}
else
{
$newnode = $this->domdoc->createTextNode($text);
$newnode->nodeValue = $text;
}
return $newnode;
}
private function dnl2array($domnodelist) {
$return = array();
$length = $domnodelist->length;
for ($i = 0; $i < $length; ++$i) {
$return[] = $domnodelist->item($i);
}
return $return;
}
}
$domdocu = new DomDocument();
$domdocu->loadHTML('
<html>
<head>
</head>
<body>
<div id="first">
%ELEMENT%
</div>
<div class="second">
%ELEMENT%
<div class="third">
%ELEMENT%
</div>
</div>
<div class="fourth">
%ELEMENT%
</div>
</body>
</html>
');
$parser = new MyParser($domdocu);
$parser->insertInto('class', 'third', 'newtesttxtxttx');
$parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'id', 'first');
$parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'class', 'second');
echo $parser;
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.30 , 8.1.22 - 8.1.28 , 8.2.9 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
</head>
<body>
<div id="first">
</div>
<div class="second">
<div class="third">
%ELEMENT%
<span>newtesttxtxttx</span></div>
</div>
<div class="fourth">
%ELEMENT%
</div>
</body>
</html>
Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
</head>
<body>
<div id="first">
</div>
<div class="second">
<div class="third">
%ELEMENT%
<span>newtesttxtxttx</span></div>
</div>
<div class="fourth">
%ELEMENT%
</div>
</body>
</html>
Output for 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.29 , 8.1.0 - 8.1.21 , 8.2.0 - 8.2.8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head></head><body>
<div id="first">
</div>
<div class="second">
<div class="third">
%ELEMENT%
<span>newtesttxtxttx</span></div>
</div>
<div class="fourth">
%ELEMENT%
</div>
</body></html>
preferences:dark mode live preview
209.73 ms | 404 KiB | 291 Q