SqlParser\Context::escape PHP Method

escape() public static method

Escapes the symbol by adding surrounding backticks.
public static escape ( array | string $str, string $quote = '`' ) : string
$str array | string The string to be escaped.
$quote string Quote to be used when escaping.
return string
    public static function escape($str, $quote = '`')
    {
        if (is_array($str)) {
            foreach ($str as $key => $value) {
                $str[$key] = static::escape($value);
            }
            return $str;
        }
        if (static::$MODE & Context::NO_ENCLOSING_QUOTES && !static::isKeyword($str, true)) {
            return $str;
        }
        if (static::$MODE & Context::ANSI_QUOTES) {
            $quote = '"';
        }
        return $quote . str_replace($quote, $quote . $quote, $str) . $quote;
    }

Usage Example

Example #1
0
 public function testEscape()
 {
     Context::setMode('ANSI_QUOTES');
     $this->assertEquals('"test"', Context::escape('test'));
     Context::setMode();
     $this->assertEquals('`test`', Context::escape('test'));
     $this->assertEquals(array('`a`', '`b`'), Context::escape(array('a', 'b')));
 }
All Usage Examples Of SqlParser\Context::escape