Skip to content

Instantly share code, notes, and snippets.

@wjzijderveld
Forked from iaindooley/xpath_escape.php
Created July 2, 2013 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wjzijderveld/5912555 to your computer and use it in GitHub Desktop.
Save wjzijderveld/5912555 to your computer and use it in GitHub Desktop.
<?php
function xpathEscape($query,$default_delim = '"')
{
if((strpos($query,'\'') !== FALSE) ||
(strpos($query,'"') !== FALSE))
{
$quotechars = array('\'','"');
$parts = array();
$current_part = '';
foreach(str_split($query) as $character)
{
if(in_array($character,$quotechars))
{
$parts[] = '\''.$current_part.'\'';
if($character == '\'')
$parts[] = '"'.$character.'"';
else
$parts[] = '\''.$character.'\'';
$current_part = '';
}
else
$current_part .= $character;
}
if($current_part)
$parts[] = '\''.$current_part.'\'';
$ret = 'concat('.implode(',',$parts).')';
}
else
$ret = $default_delim.$query.$default_delim;
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment