Skip to content

Instantly share code, notes, and snippets.

@chrif
Forked from iaindooley/xpath_escape.php
Created February 10, 2012 02:15
Show Gist options
  • Save chrif/1785543 to your computer and use it in GitHub Desktop.
Save chrif/1785543 to your computer and use it in GitHub Desktop.

Revisions

  1. chrif revised this gist Jun 7, 2012. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions xpath_escape.php
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,5 @@ function xpathEscape($query, $default_delim = '"')
    $delim = $apos[$i] ? '"' : "'";
    $part = $delim . $part . $delim;
    }
    // concat() must have two or more parts
    if (count($parts) == 1)
    $parts[] = $delim . $delim;
    return 'concat(' . implode(',', $parts) . ')';
    return 'concat("",' . implode(',', $parts) . ')';
    }
  2. chrif revised this gist Jun 6, 2012. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion xpath_escape.php
    Original file line number Diff line number Diff line change
    @@ -9,5 +9,8 @@ function xpathEscape($query, $default_delim = '"')
    $delim = $apos[$i] ? '"' : "'";
    $part = $delim . $part . $delim;
    }
    // concat() must have two or more parts
    if (count($parts) == 1)
    $parts[] = $delim . $delim;
    return 'concat(' . implode(',', $parts) . ')';
    }
    }
  3. chrif revised this gist Feb 10, 2012. 1 changed file with 12 additions and 39 deletions.
    51 changes: 12 additions & 39 deletions xpath_escape.php
    Original file line number Diff line number Diff line change
    @@ -1,40 +1,13 @@
    <?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;
    }
    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) . ')';
    }
  4. iaindooley revised this gist Aug 21, 2011. No changes.
  5. iaindooley renamed this gist Aug 21, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. iaindooley revised this gist Aug 19, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generic_concat.php
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ function xpathEscape($query,$default_delim = '"')
    }

    if($current_part)
    $parts[] = $current_part;
    $parts[] = '\''.$current_part.'\'';

    $ret = 'concat('.implode(',',$parts).')';
    }
  7. iaindooley revised this gist Aug 19, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generic_concat.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    <?php
    function genericXpathQuoteConcatenation($query,$default_delim = '"')
    function xpathEscape($query,$default_delim = '"')
    {

    if((strpos($query,'\'') !== FALSE) ||
  8. iaindooley created this gist Aug 19, 2011.
    40 changes: 40 additions & 0 deletions generic_concat.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <?php
    function genericXpathQuoteConcatenation($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;
    }