Skip to content

Unable to get comment contents in SimpleXML #12169

Description

@ndossche

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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