Skip to content

Instantly share code, notes, and snippets.

@drslump
Created July 16, 2010 12:24
Show Gist options
  • Save drslump/478306 to your computer and use it in GitHub Desktop.
Save drslump/478306 to your computer and use it in GitHub Desktop.
<?php
// Escapes a string (if needed) using the concat function for its use
// in xpath expressions.
// Example:
// $xpath = '/*[@value=' . escape_string_xpath("this is "foo's\"!") . ']';
// Becomes:
// /*[@value=concat('this is "foo', "'", 's"!')]
function escape_string_xpath($value) {
if (strpos($value, '"') === false) {
return '"' . $value . '"';
} else if (strpos($value, "'") === false) {
return "'" . $value . "'";
}
return 'concat("' . str_replace('"', '", \'"\', "', $value) . '")';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment