-
-
Save chrif/1785543 to your computer and use it in GitHub Desktop.
Function to escape single and double quotes in XPath queries using PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) . ')'; | |
} |
You're right. Thank you for telling me.
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
This function has a bug, see my fork for details.
https://gist.github.com/2883026