Skip to content

Unable to get comment contents in SimpleXML #12169

Closed
@nielsdos

Description

@nielsdos

Description

Bug report or feature request, I don't know for sure... Could be considered either?

Anyway:

<?php

$sxe = simplexml_load_string(<<<XML
<?xml version="1.0"?>
<container>
    <bar>hi</bar>
    <!-- comment -->
</container>
XML);

var_dump((string) $sxe->bar);
var_dump((string) $sxe->comment);

Gives output:

string(2) "hi"
string(0) ""

But I expected:

string(2) "hi"
string(7) "comment"

Looking at the documentation for __toString: https://www.php.net/manual/en/simplexmlelement.tostring.php

Returns text content that is directly in this element. (...)

Well, the confusing thing is that every XML node is called a SimpleXMLElement in ext/simplexml, even if they're not XML elements. But that aside, if I read the docs as "Returns text content that is directly in this node." then I would expect the cast to give me the comment contents, as that's the text inside of it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions