I’ve been working with the Basecamp API to plugin our IRC bot that we use for time tracking and I’m astounded to learn that escaping single and/or double quotes for XPath queries in PHP does not have a well documented, best practices solution.

In fact, it seems as though this is not peculiar to PHP. I took a look around and found this excellent article by “Kushal” (s/he doesn’t have their full name on their blog):

https://web.archive.org/web/20081006202505/http://kushalm.com/the-perils-of-xpath-expressions-specifically-escaping-quotes

I’ve produced a PHP solution for the general escaping/concatenation problem:

Anyone got a better/more elegant solution? I’m always a big fan of code golf 🙂