This is really just a syntax question.
I have a PHP script that parses my WordPress feed and returns the latest posts. I also want my script to parse the # of comments, but the WordPress feed XML object for number of comments has a colon in it (slash:comments). It causes the following error:
Parse error: syntax error, unexpected
‘:’ in … on line …
I have tried each of the following without luck:
$xml->slash:comments
$comments = 'slash:comments'
$xml->$comments
$xml->slash.':'.comments
$xml->{slash:comments}
$xml->{'slash:comments'}
How do I parse an object with a colon?
Alternatively, you can use xpath() to access the nodes. Given the following as an xml string:
You could do this:
As you can see, to get the nodes with colons, you may use xpath() with a relative path to the node.
A variable in PHP can never have a colon in it. Therefore, you should check your XML parser to see how it handles colons.
Use str_replace to remove the colons from the string and allow simplexml_load_string to function as normal.
For example:
… should return the published date, title, and number of comments of the posts listed in a WordPress feed. My code is more advanced, but this illustrates the workaround.
Thank you, Arda Xi, for your help!