|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-06-29 06:16 UTC] jfowlie at navarik dot com
Description:
------------
I was attempting to update the content of some anchor elements by setting the nodeValue. It doesn't seem to matter how high I set the max_execution_time limit, if there is a entity in the nodeValue, PHP just hangs until it times out. (It works just fine with & though...)
Reproduce code:
---------------
<?php
$xhtml = <<<XHTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<a href="#" id="id-goes-here">Link 1</a>
<a href="#" id="id-2-goes-here">Link 2 &</a>
<a href="#" id="id-3-goes-here">Link 3 </a>
</body>
</html>
XHTML;
$dom = new DOMDocument('1.0','iso-8859-1');
// I tried it with this set to true or false
//$xml->preserveWhiteSpace = FALSE;
// You need this, otherwise the nbsp entity is not valid
$dom->resolveExternals = TRUE;
$dom->loadXML($xhtml);
$bookmarks = $dom->getElementsByTagName('a');
$count = 0;
foreach($bookmarks as $bookmark_node){
++$count;
$id_value = $bookmark_node->getAttribute('id');
print "Id value: ".$id_value."<br />";
print "Node value: ".$bookmark_node->nodeValue."<br />";
print "Setting nodeValue to 'test$count'<br />";
$bookmark_node->nodeValue = 'test'.$count;
print "Node value has been set! ({$bookmark_node->nodeValue})<hr />";
}
?>
Expected result:
----------------
Id value: id-goes-here
Node value: Link 1
Setting nodeValue to 'test1'
Node value has been set! (test1)
Id value: id-2-goes-here
Node value: Link 2 &
Setting nodeValue to 'test2'
Node value has been set! (test2)
Id value: id-3-goes-here
Node value: Link 3
Setting nodeValue to 'test3'
Node value has been set! (test3)
Actual result:
--------------
Id value: id-goes-here
Node value: Link 1
Setting nodeValue to 'test1'
Node value has been set! (test1)
Id value: id-2-goes-here
Node value: Link 2 &
Setting nodeValue to 'test2'
Node value has been set! (test2)
Id value: id-3-goes-here
Node value: Link 3
Setting nodeValue to 'test3'
Fatal error: Maximum execution time of 30 seconds exceeded in nbsp-dom-test.php on line 39
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 02:00:01 2025 UTC |
In case anyone wants a workaround... Check that a node hasChildNodes and that the firstChild->nodeType is XML_TEXT_NODE. Then you can just set the value of the node with firstChild->nodeValue I.e. change the foreach loop in the reproduce code to: foreach($bookmarks as $bookmark_node){ ++$count; $id_value = $bookmark_node->getAttribute('id'); print "Id value: ".$id_value."<br />\n"; if ($bookmark_node->hasChildNodes() && $bookmark_node->firstChild->nodeType == XML_TEXT_NODE) { print "Node value: ".$bookmark_node->firstChild->nodeValue."<br />\n"; print "Setting nodeValue to 'test$count'<br />\n"; $bookmark_node->firstChild->nodeValue = 'test'.$count; print "Node value has been set! ({$bookmark_node->firstChild->nodeValue})<hr />\n"; } }