This is actually only a syntax question.

I've got a PHP script that parses my WordPress feed and returns the most recent posts. I additionally want my script to parse the # of comments, however the WordPress feed XML object for quantity of comments includes a colon inside it (slash:comments). It causes the next error:

Parse error: syntax error, unpredicted ':' ins.. online ...

I've attempted each one of the following without luck:

$xml->slash:comments

$comments = 'slash:comments'
$xml->$comments

$xml->slash.':'.comments
$xml->{slash:comments}
$xml->{'slash:comments'}

How do you parse an item having a colon?

$string = file_get_contents("http://domain.tld/?feed=rss2");
$string = str_replace('slash:comments','slashcomments',$string);

$xml = simplexml_load_string($string);

Use str_replace to get rid of the colons in the string and permit simplexml_load_string to operate normally.

For instance:

$string = file_get_contents("http://domain.tld/?feed=rss2");
$string = str_replace('slash:comments','slashcomments',$string);
$xml = simplexml_load_string($string);
foreach ($xml->channel->item as $val) {
    echo $val->pubDate.'<br />';
    echo $val->title.'<br />';
    echo $val->slashcomments.'<br /><br />';
}

... should return the released date, title, and quantity of comments from the posts indexed by a WordPress feed. My code is much more advanced, but this demonstrates the workaround.

Thanks, Arda Xi, for the help!

Alternatively, you should use xpath() to gain access to the nodes. Because of the following being an xml string:

<entry>
  <id>http://gdata.youtube.com/feeds/api/videos/xyz12345678</id>
  <published>2007-01-17T23:41:00.000Z</published>
  <updated>2010-11-14T03:52:25.000Z</updated>
  <yt:location>Mount Washington Observatory, NH</yt:location>
  <media:group>
    <media:title type='plain'>Example of a Title</media:title>
    <media:duration seconds='126'/>
  </media:group>
</entry>

You could do this this:

$xml = simplexml_load_string(*xmlstring_from_above*);

$location = $xml->xpath('yt:location');
echo($location[0]); // output: "Mount Washington Observatory, NH"

$title = $xml->xpath('media:group/media:title');
echo($title[0]); // output: "Example of a Title"

$duration = $xml->xpath('media:group/media:duration');
echo($duration[0]['seconds']); // output: "126"

As you can tell, to obtain the nodes with colons, you can utilize xpath() having a relative road to the node.

A flexible in PHP can't ever possess a colon inside it. Therefore, you can examine your XML parser to determine the way it handles colons.