Escapes a string when it contains special characters (RFC3501)

@param string  $string       IMAP string
@param boolean $force_quotes Forces string quoting (for atoms)

@return string String atom, quoted-string or string literal
@todo lists
515206
public static function escape($string, $force_quotes=false)
    {
        if ($string === null) {
            return 'NIL';
        }

        if ($string === '') {
            return '""';
        }

        // atom-string (only safe characters)
        if (!$force_quotes && !preg_match('/[\x00-\x20\x22\x25\x28-\x2A\x5B-\x5D\x7B\x7D\x80-\xFF]/', $string)) {
            return $string;
        }

        // quoted-string
        if (!preg_match('/[\r\n\x00\x80-\xFF]/', $string)) {
            return '"' . addcslashes($string, '\\"') . '"';
        }

        // literal-string
        return sprintf("{%d}\r\n%s", strlen($string), $string);
    }
1050425
public function escape($string, $functions = null)
    {
        static $flags;

        if (!isset($flags)) {
            $flags = ENT_QUOTES | ENT_SUBSTITUTE;
        }

        if ($functions) {
            $string = $this->batch($string, $functions);
        }

        return htmlspecialchars($string, $flags, 'UTF-8');
    }
804668
public static function quote($string, $quote = array('"', '"'))
	{
		$quote = (array) $quote;

		if (empty($quote[1]))
		{
			$quote[1] = $quote[0];
		}

		return strpos($string, $quote[0])!== 0 ? ($quote[0] . $string . $quote[1]) : $string;
	}
459265
public function escapeAttr($string, $encoding = 'utf-8') {
		
		$current = Escape :: getEncoding();
		Escape :: setEncoding($encoding);
		$result =  Escape :: attr($string);
		Escape :: setEncoding($current);

		return $result;
	}
963285
public static function hardDecode($string) {
		$data = htmlspecialchars_decode($string);
		$data = html_entity_decode($data);
		$data = strip_tags($data);
		$data = chop(Utility::convert_smart_quotes($data));
		$data = str_replace(array("\r", "\n", "	"), "", $data);

		return ($data);
	}
805851
public function escape($string, $flags = 'html')
    {
        switch ($flags) {
            case 'url' :
                return urlencode($string);
            case 'quotes' :
                return addcslashes($string, '"\'\\');
            case 'phone' :
                if (strlen($string) < 8) {
                    return substr($string, 0, -4) . '****';
                }
                return substr($string, 0, 3) . '****' . substr($string, -4);
            case 'json' :
                return json_encode($string);
            case 'html' :
            default :
                return htmlspecialchars($string, ENT_QUOTES|ENT_HTML5);
        }
    }
151788
public function escape_string($string)
    {
        if (!$this->connection) {
            return $string;
        }

        if ($string === null) {
            return $string;
        }
        return $this->connection->escape_string($string);
    }
1250144
private String escapeSpecialCharacters(final String string) {
		final StringBuilder escaped = new StringBuilder();
		for (final char character : string.toCharArray()) {
			if (character == '%' || character == '_' || character == '\\') {
				escaped.append('\\');
			}
			escaped.append(character);
		}
		return escaped.toString();
	}
463785
private static function escape( $string )
	{
		$replace	= array(
			'\\'	=> '\\\\',
			'"'	=> '\"',
			'/'	=> '\/',
			"\b"	=> '\b',
			"\f"	=> '\f',
			"\n"	=> '\n',
			"\r"	=> '\r',
			"\t"	=> '\t',
			"\u"	=> '\u'
			);
		$string	= str_replace( array_keys( $replace ), array_values( $replace ), $string );
		return $string;
	}
1353458
public function escape($string)
    {
        if(!$this->isConnected())
        {
            $this->connect();
            if(!$this->isConnected()) return false;
        }

        return $this->_escape($string);
    }
518260
protected function notInsideQuotes($string, $quote = true)
    {
        if ($quote) {
            $string = preg_quote($string);
        }

        return
            // double quotes - ignore "" and everything inside quotes for example " abc \"err "
            '(?:""|"(?:[^"]|\\")*?[^\\\]")(*SKIP)(*F)|' . $string .
            '|' .
            // single quotes - ignore '' and everything inside quotes for example ' abc \'err '
            '(?:\\\'\\\'|\'(?:[^\']|\\\')*?[^\\\]\')(*SKIP)(*F)|' . $string;
    }
705602
protected function escapeLatex($string)
	{
		if ($this->_escaper === null) {
			$this->_escaper = new TextToLatex();
		}
		return $this->_escaper->convert($string);
	}
554943
public function escapeString(string $input, bool $quote = true): string
    {
        $resourceType = $this->resource->getType();
        $resourceObject = $this->resource->getObject();
        if ($resourceType == Resource::TYPE_MYSQL_LINK) {
            $input = $resourceObject->escape_string($input);
        } elseif ($resourceType == Resource::TYPE_PGSQL_LINK) {
            $input = pg_escape_string($resourceObject, $input);
        }

        if ($quote) {
            $input = $this->quote($input);
        }

        return $input;
    }
567674
public static function escapeString( $string_data, $options = NULL )
    {
        if ( !$options ){
            $options = array(
                            'quote_style' => 'ENT_QUOTES',
                        );
        }

        $quote_style = ENT_NOQUOTES;
        if ( isset($options['quote_style']) && $options['quote_style'] == 'ENT_QUOTES' ){
            $quote_style = ENT_QUOTES;
        }

        $str = htmlspecialchars( $string_data, $quote_style );

        return $str;
    }
1281644
public function escapeAttribute(?string $string, string $charset): string
	{
		if($charset !== 'UTF-8')
		{
			$string = mb_convert_encoding($string, 'UTF-8', $charset);
		}

		$string = preg_replace_callback('/[^a-z0-9,\.\-_]/iSu', [$this, 'attributeEscaper'], $string);

		if($charset !== 'UTF-8')
		{
			$string = mb_convert_encoding($string, $charset, 'UTF-8');
		}

		return $string;
	}
149992
public static String escape(String string) {
        if (isEmpty(string)) return "";
        StringBuilder sb = new StringBuilder();
        char[] chars = string.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i == chars.length - 1 || chars[i] != '\r' || chars[i + 1] != '\n') {
                sb.append(escape(chars[i]));
            }
        }
        return sb.toString();
    }
557931
final public function escapeStringForLike(string $inputString, string $escapeChar) : string
    {
        return preg_replace(
            '~([' . preg_quote($this->getLikeWildcardCharacters() . $escapeChar, '~') . '])~u',
            addcslashes($escapeChar, '\\') . '$1',
            $inputString
        );
    }