php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52553 the result is different between php versions
Submitted: 2010-08-06 10:43 UTC Modified: 2015-05-22 02:40 UTC
From: dataget2001 at yahoo dot co dot jp Assigned: cmb (profile)
Status: Not a bug Package: SimpleXML related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2010-08-06 10:43 UTC] dataget2001 at yahoo dot co dot jp
Description:
------------
the result of test script below is different between php version 5.1.6 and version 5.3.2.


Test script:
---------------
<?php
var_dump(phpversion());

$foo1 = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root />');
$foo1->addChild("x", htmlspecialchars("<test />"));

$foo2 = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root><x /></root>');
$foo2->x = htmlspecialchars("<test />");

var_dump($foo1->x);
var_dump($foo2->x);
var_dump($foo1->asXML() === $foo2->asXML());

?>

Expected result:
----------------
string(5) "5.1.6"
object(SimpleXMLElement)#3 (1) {
  [0]=>
  string(8) "<test />"
}
object(SimpleXMLElement)#3 (1) {
  [0]=>
  string(8) "<test />"
}
bool(true)



Actual result:
--------------
string(5) "5.3.2"
object(SimpleXMLElement)#3 (1) {
  [0]=>
  string(8) "<test />"
}
object(SimpleXMLElement)#3 (1) {
  [0]=>
  string(14) "&lt;test /&gt;"
}
bool(false)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-11 18:11 UTC] felipe@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: rrichards
 [2010-09-11 18:11 UTC] felipe@php.net
This report remember another report. Need to find it.
 [2010-09-11 19:02 UTC] felipe@php.net
The bug related to this stuff is the bug #44478

According to the test committed for the mentioned bug (http://svn.php.net/viewvc/php/php-src/trunk/ext/simplexml/tests/bug44478.phpt?revision=255607&view=markup) it seems expected.

To get the expected behavior, use addChild().

<?php

$xml_element = new simpleXMLElement('<root></root>');
$xml_element->addchild('node1', 'a &#38; b');
var_dump($xml_element->node1);

/*
object(SimpleXMLElement)#2 (1) {
  [0]=>
  string(5) "a & b"
}
*/
 [2015-05-22 02:40 UTC] cmb@php.net
-Status: Assigned +Status: Not a bug -Assigned To: rrichards +Assigned To: cmb
 [2015-05-22 02:40 UTC] cmb@php.net
> the result of test script below is different between php version
> 5.1.6 and version 5.3.2.

That is because the relevant bug has been fixed in PHP 5.2.6[1].
You don't have to escape HTML entities (actually, you should not)
when assigning to a node; that is supposed to be done by
SimpleXML[2].

[1] <http://3v4l.org/vv8CB>
[2] <http://3v4l.org/U1tvv>
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Dec 09 02:05:54 2022 UTC