Wednesday, July 07, 2010

Colonary Surgery

I don't get the opportunity to do a vast amount of XML parsing, but when I do I use SimpleXML because it's normally built into PHP and is just so...well, simple. Unfortunately, there are some things SimpleXML can't handle. One of these is colons in tag names. Google's API's use colons in abundance, such as when giving the ID of a YouTube video: <yt:videoid>.


For now, I don't have time to learn another parser, so here's a quick and dirty solution I found in a few places on the web: remove the colons!

$newXmlStr = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xmlStr);


Works well for me, though I'm sure if you are one of those who parse vast amounts of XML, this solution would be too low-performance. But for the occasional fetching of a YouTube video or Picasa album, it can be a very useful line of code.

Labels: , ,