Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function to escape single and double quotes in XPath queries using PHP
<?php
function xpathEscape($query, $default_delim = '"')
{
if (strpos($query, $default_delim) === false)
return $default_delim . $query . $default_delim;
preg_match_all("#(?:('+)|[^']+)#", $query, $matches);
list($parts, $apos) = $matches;
foreach ($parts as $i => &$part) {
$delim = $apos[$i] ? '"' : "'";
$part = $delim . $part . $delim;
}
return 'concat("",' . implode(',', $parts) . ')';
}
@jaywilliams

This comment has been minimized.

Copy link

jaywilliams commented Jun 6, 2012

This function has a bug, see my fork for details.
https://gist.github.com/2883026

@chrif

This comment has been minimized.

Copy link
Owner Author

chrif commented Jun 6, 2012

You're right. Thank you for telling me.

@jaywilliams

This comment has been minimized.

Copy link

jaywilliams commented Jun 7, 2012

I see you've optimized my fork even further. I like your elegant solution to the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.