public static function xpathLiteral($s)
    {
        if (false === strpos($s, "'")) {
            return sprintf("'%s'", $s);
        }

        if (false === strpos($s, '"')) {
            return sprintf('"%s"', $s);
        }

        $string = $s;
        $parts = [];
        while (true) {
            if (false !== $pos = strpos($string, "'")) {
                $parts[] = sprintf("'%s'", substr($string, 0, $pos));
                $parts[] = "\"'\"";
                $string = substr($string, $pos + 1);
            } else {
                $parts[] = "'$string'";
                break;
            }
        }

        return sprintf('concat(%s)', implode(', ', $parts));
    }
public static function xpathLiteral($s)
    {
        if (false === strpos($s, "'")) {
            return sprintf("'%s'", $s);
        }

        if (false === strpos($s, '"')) {
            return sprintf('"%s"', $s);
        }

        $string = $s;
        $parts = [];
        while (true) {
            if (false !== $pos = strpos($string, "'")) {
                $parts[] = sprintf("'%s'", substr($string, 0, $pos));
                $parts[] = "\"'\"";
                $string = substr($string, $pos + 1);
            } else {
                $parts[] = "'$string'";
                break;
            }
        }

        return sprintf('concat(%s)', implode(', ', $parts));
    }
public static function xpathLiteral ( $ s ) { if ( false === strpos ( $ s , "'" ) ) { return sprintf ( "'%s'" , $ s ) ; } if ( false === strpos ( $ s , '"' ) ) { return sprintf ( '"%s"' , $ s ) ; } $ string = $ s ; $ parts = [ ] ; while ( true ) { if ( false !== $ pos = strpos ( $ string , "'" ) ) { $ parts [ ] = sprintf ( "'%s'" , substr ( $ string , 0 , $ pos ) ) ; $ parts [ ] = "\"'\"" ; $ string = substr ( $ string , $ pos + 1 ) ; } else { $ parts [ ] = "'$string'" ; break ; } } return sprintf ( 'concat(%s)' , implode ( ', ' , $ parts ) ) ; }
Converts string for XPath expressions.

Escaped characters are: quotes (") and apostrophe (').

Examples:

echo Crawler::xpathLiteral('foo " bar');
//prints 'foo " bar'

echo Crawler::xpathLiteral("foo ' bar");
//prints "foo ' bar"

echo Crawler::xpathLiteral('a\'b"c');
//prints concat('a', "'", 'b"c')


@param string $s String to be escaped

@return string Converted string
Converts string for XPath expressions .