Helper method to escape all ZendSearch special characters.

@param string $str

@return string
324621
protected function escape($str)
	{
		$str = str_replace('\\', '\\\\', $str);
		$str = str_replace('+', '\+', $str);
		$str = str_replace('-', '\-', $str);
		$str = str_replace('&&', '\&&', $str);
		$str = str_replace('||', '\||', $str);
		$str = str_replace('!', '\!', $str);
		$str = str_replace('(', '\(', $str);
		$str = str_replace(')', '\)', $str);
		$str = str_replace('{', '\{', $str);
		$str = str_replace('}', '\}', $str);
		$str = str_replace('[', '\[', $str);
		$str = str_replace(']', '\]', $str);
		$str = str_replace('^', '\^', $str);
		$str = str_replace('"', '\"', $str);
		$str = str_replace('~', '\~', $str);
		$str = str_replace('*', '\*', $str);
		$str = str_replace('?', '\?', $str);
		$str = str_replace(':', '\:', $str);
		
		$str = str_ireplace(
			array(' and ', ' or ', ' not ', ' to '),
			'',
			$str
		);
		
		return $str;
	}
1351212
public function _safe_js_string($str)
    {
        $length = strlen($str);
        $ret = '';
        for ($i = 0; $i < $length; $i++) {
            switch ($str[$i]) {
                case "\r":
                    $ret .= '\\r';
                    break;
                case "\n":
                    $ret .= '\\n';
                    break;
                case '"':
                case "'":
                case '<':
                case '>':
                case '&':
                case '\\':
                    $ret .= '\x'.dechex(ord($str[$i]));
                    break;
                default:
                    $ret .= $str[$i];
                    break;
            }
        }

        return $ret;
    }
1148109
public function escape($str = '')
    {
        $config = \HTMLPurifier_Config::createDefault();
        $config->set('Cache.SerializerPath', $this->cachePath);
        if (!empty($this->config) && is_array($this->config) && count($this->config) > 0) {
            foreach ($this->config as $key => $value) {
                $config->set($key, $value);
            }
        }
        $purifier = new \HTMLPurifier($config);
        $clean    = $purifier->purify($str);

        return $clean;
    }
1073068
private static function UnescapeReplacementString( $str )
	{
		$result = "";
		$charArr = str_split ( $str );

		for ( $k = 0; $k < count( $charArr ); $k++ )
		{
			if ( $charArr[ $k ] == '\\' )
			{
				if ( $k == count( $charArr ) - 1 )
					throw XPath2Exception::withErrorCodeAndParam( "FORX0004", Resources::FORX0004, $str );

					switch ( $charArr[ $k + 1 ] )
					{
						case 'n':
							$result .= "\n";
							break;

						case 'r':
							$result .="\r";
							break;

						case 't':
							$result .= "\t";
							break;

						case 'a':
							$result .= "\a";
							break;

						case '\\':
							$result .= "\\";
							break;

						default:
							throw XPath2Exception::withErrorCodeAndParam( "FORX0004", Resources::FORX0004, $str );
					}
					$k++;
			}
			else
				$result .= $charArr[ $k ];
		}
		return $result;
	}
1074652
protected function _code($str) {
		$self=$this;
		return preg_replace_callback(
			'/`` (.+?) ``|(?<!\\\\)`(.+?)(?!\\\\)`/',
			function($expr) use($self) {
				return '<code>'.
					$self->esc(empty($expr[1])?$expr[2]:$expr[1]).'</code>';
			},
			$str
		);
	}
800553
static function ldap_escape( $str, $for_dn = false )
    {
        // see: RFC2254
        // http://msdn.microsoft.com/en-us/library/ms675768(VS.85).aspx
        // http://www-03.ibm.com/systems/i/software/ldap/underdn.html

        if ( $for_dn )
        {
            $metaChars = array( ',', '=', '+', '<', '>', ';', '\\', '"', '#' );
            $quotedMetaChars = array( '\2c', '\3d', '\2b', '\3c', '\3e', '\3b', '\5c', '\22', '\23' );
        }
        else
        {
            $metaChars = array( '*', '(', ')', '\\', chr(0) );
            $quotedMetaChars = array( '\2a', '\28', '\29', '\5c', '\00' );
        }

        return str_replace( $metaChars, $quotedMetaChars, $str );
    }
1085838
static function escape_string($str)
    {
        if (is_array($str) && count($str) > 1) {
            foreach ($str as $idx => $val)
                $str[$idx] = self::escape_string($val);

            return '[' . implode(',', $str) . ']';
        }
        else if (is_array($str)) {
            $str = array_pop($str);
        }

        // multi-line string
        if (preg_match('/[\r\n\0]/', $str)) {
            return sprintf("text:\n%s\n.\n", self::escape_multiline_string($str));
        }
        // quoted-string
        else {
            return '"' . addcslashes($str, '\\"') . '"';
        }
    }
452723
public static function alpha($str)
    {
        if(is_array($str)){
            foreach($str as $key=>$val){
                $str[$key] = self::alpha($val);
            }
            return $str;
        }
        return preg_replace('/[^a-z]/i', '', $str);
    }
782214
function _filter_uri($str) {
		if ($str != '' && $this->permitted_uri_chars != '' && $this->enable_query_strings == FALSE) {
			// preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
			// compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
			if (!preg_match("|^[" . str_replace(array ('\\-','\-' ), '-', preg_quote($this->permitted_uri_chars, '-')) . "]+$|i", $str)) {
				show_error('The URI you submitted has disallowed characters.', 400);
			}
		}

		// Convert programatic characters to entities
		$bad=array ('$','(',')','%28','%29' );
		$good=array ('&#36;','&#40;','&#41;','&#40;','&#41;' );

		return str_replace($bad, $good, $str);
	}
560056
public static function escapeString($str, $allow_wildcards=false) {
        $needles = [
            '\\', '+', '-', '=', '&&', '||', '>', '<', '!', '(', ')',
            '{', '}', '[', ']', '^', '"', '~', ':', '/', ' '
        ];
        $replacements = [
            '\\\\', '\\+', '\\-', '\\=', '\\&&', '\\||', '\\>', '\\<', '\\!', '\\(', '\\)',
            '\\{', '\\}', '\\[', '\\]', '\\^', '\\"', '\\~', '\\:', '\\/', '\\ '
        ];
        if(!$allow_wildcards) {
            $needles = array_merge($needles, ['*', '?']);
            $replacements = array_merge($replacements, ['\\*', '\\?']);
        }
        return str_replace($needles, $replacements, $str);
    }
1255152
public static function sanitizeFilename($str)
    {
        $replace_chars = array(
            'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
            'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
            'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
            'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
            'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
            'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
            'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f'
        );
        $str = strtr($str, $replace_chars);
        $str = preg_replace(array('/[\&]/', '/[\@]/', '/[\#]/'), array('-and-', '-at-', '-number-'), $str);
        $str = preg_replace('/[^(\x20-\x7F)]*/','', $str); // removes any special chars we missed
        $str = str_replace(' ', '-', $str); // convert space to hyphen
        $str = str_replace('\'', '', $str); // removes apostrophes
        $str = preg_replace('/[^\w\-\.]+/', '', $str); // remove non-word chars (leaving hyphens and periods)
        $str = preg_replace('/[\-]+/', '-', $str); // converts groups of hyphens into one

        return strtolower($str);
    }
773332
public static function decode(String $str) : String
    {
        return str_replace(array_values(self::$phpTagChars), array_keys(self::$phpTagChars), $str);
    }
1299234
protected function encode($str)
    {
        $args = func_get_args();
        array_shift($args);
        $args = array_map(
            function ($str) {
                return htmlspecialchars($str, ENT_QUOTES);
            },
            $args
        );
        array_unshift($args, $str);

        return call_user_func_array('sprintf', $args);
    }
1202540
public static function encodeIRI( $str )
    {
        $codec = eZTextCodec::instance( false, 'utf-8' );
        $str = $codec->convertString( $str ); // Make sure the string is in utf-8
        $out = explode( "/", $str ); // Don't encode the slashes
        foreach ( $out as $i => $o )
        {
            if ( preg_match( "#^[\(]([a-zA-Z0-9_]+)[\)]#", $o, $m ) )
            {
                // Don't encode '(' and ')' in user parameters
                $out[$i] = '(' . urlencode( $m[1] ) . ')';
            }
            else
            {
                $out[$i] = urlencode( $o ); // Let PHP do the rest
            }
        }
        $tmp = join( "/", $out );
        // Don't encode '~' in URLs
        $tmp = str_replace( '%7E', '~', $tmp );
        return $tmp;
    }
74633
private function toCamelCase($str): string
    {
        $func = function ($c) {
            return strtoupper($c[1]);
        };

        return preg_replace_callback('/_([a-z])/', $func, $str);
    }
920536
public function _safe_js_string($str)
    {
        $l = strlen($str);
        $ret = '';
        for ($i = 0; $i < $l; ++$i) {
            switch ($str[$i]) {
                case "\r":
                    $ret .= '\\r';
                    break;
                case "\n":
                    $ret .= '\\n';
                    break;
                case '"':
                case "'":
                case '<':
                case '>':
                case '&':
                case '\\':
                    $ret .= '\x'.dechex(ord($str[$i]));
                    break;
                default:
                    $ret .= $str[$i];
                    break;
            }
        }

        return $ret;
    }
701798
private static function escapeString($str)
    {
        if (strlen($str) === 0) {
            return $str;
        }

        $str = str_replace(array(
            "\r",
            "\t",
            "\\",
            "\$",
            "\v",
            "\e",
            "\f",
            "\""
        ), array(
            '\r',
            '\t',
            '\\\\',
            '\$',
            '\v',
            '\e',
            '\f',
            '\"'
        ), $str);

        $str = str_replace("\n", "\\n\"\n\"", $str);

        $result = '';
        $str = str_split($str, 1);
        foreach ($str as $chr) {
            if (!ctype_print($chr) && $chr !== "\n") {
                $result .= '\\' . decoct(ord($chr));
            } else {
                $result .= $chr;
            }
        }
        if (substr($result, -5) === "\\n\"\n\"") {
            $result = substr($result, 0, -5) . "\\n";
        }
        return $result;
    }