-
-
Save grayguest/69f3af492113a12a5b6081ee1eb1a126 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) . ')'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment