Encodes text via quoted-printable encoding.

@param string $text   The text to encode (UTF-8).
@param string $eol    The EOL sequence to use.
@param integer $wrap  Wrap a line at this many characters.

@return string  The quoted-printable encoded string.
493786
public static function encode($text, $eol = "\n", $wrap = 76)
    {
        $fp = fopen('php://temp', 'r+');
        stream_filter_append(
            $fp,
            'convert.quoted-printable-encode',
            STREAM_FILTER_WRITE,
            array(
                'line-break-chars' => $eol,
                'line-length' => $wrap
            )
        );
        fwrite($fp, $text);
        rewind($fp);
        $out = stream_get_contents($fp);
        fclose($fp);

        return $out;
    }
286555
public function encodeString($str, $encoding = 'base64')
    {
        $encoded = '';
        switch (strtolower($encoding)) {
            case 'base64':
                $encoded = chunk_split(
                    base64_encode($str),
                    static::STD_LINE_LENGTH - strlen(static::$LE),
                    static::$LE
                );
                break;
            case '7bit':
            case '8bit':
                $encoded = static::normalizeBreaks($str);
                // Make sure it ends with a line break
                if (substr($encoded, -(strlen(static::$LE))) != static::$LE) {
                    $encoded .= static::$LE;
                }
                break;
            case 'binary':
                $encoded = $str;
                break;
            case 'quoted-printable':
                $encoded = $this->encodeQP($str);
                break;
            default:
                $this->setError($this->lang('encoding') . $encoding);
                break;
        }

        return $encoded;
    }
404636
public function setText($text)
	{
		if (!is_array($text) && !$text instanceof self) {
			$text = htmlspecialchars((string)$text, ENT_NOQUOTES, 'UTF-8');
		}

		return $this->setHtml($text);
	}
821568
public static function toIso88591Email($text, $wrap = 0)
    {
        $ISO88591_CONV = [
            194 => [ // C2
                161=>'=A1' /*¡*/, 162=>'=A2' /*¢*/, 163=>'=A3' /*£*/, 164=>'=A4' /*¤*/, 165=>'=A5' /*¥*/,
                166=>'=A6' /*¦*/, 167=>'=A7' /*§*/, 168=>'=A8' /*¨*/, 169=>'=A9' /*©*/, 170=>'=AA' /*ª*/,
                171=>'=AB' /*«*/, 172=>'=AC' /*¬*/, 173=>'=AD' /* */, 174=>'=AE' /*®*/, 175=>'=AF' /*¯*/,
                176=>'=B0' /*°*/, 177=>'=B1' /*±*/, 178=>'=B2' /*²*/, 179=>'=B3' /*³*/, 180=>'=B4' /*´*/,
                181=>'=B5' /*µ*/, 182=>'=B6' /*¶*/, 183=>'=B7' /*·*/, 184=>'=B8' /*¸*/, 185=>'=B9' /*¹*/,
                186=>'=BA' /*º*/, 187=>'=BB' /*»*/, 188=>'=BC' /*¼*/, 189=>'=BD' /*½*/, 190=>'=BE' /*¾*/,
                191=>'=BF' /*¿*/, 160 => '=A0' /* */,
            ],
            195 => [ // C3
                128=>'=C0' /*À*/, 129=>'=C1' /*Á*/, 130=>'=C2' /*Â*/, 131=>'=C3' /*Ã*/, 132=>'=C4' /*Ä*/,
                133=>'=C5' /*Å*/, 134=>'=C6' /*Æ*/, 135=>'=C7' /*Ç*/, 136=>'=C8' /*È*/, 137=>'=C9' /*É*/,
                138=>'=CA' /*Ê*/, 139=>'=CB' /*Ë*/, 140=>'=CC' /*Ì*/, 141=>'=CD' /*Í*/, 142=>'=CE' /*Î*/,
                143=>'=CF' /*Ï*/, 144=>'=D0' /*Ð*/, 145=>'=D1' /*Ñ*/, 146=>'=D2' /*Ò*/, 147=>'=D3' /*Ó*/,
                148=>'=D4' /*Ô*/, 149=>'=D5' /*Õ*/, 150=>'=D6' /*Ö*/, 151=>'=D7' /*×*/, 152=>'=D8' /*Ø*/,
                153=>'=D9' /*Ù*/, 154=>'=DA' /*Ú*/, 155=>'=DB' /*Û*/, 156=>'=DC' /*Ü*/, 157=>'=DD' /*Ý*/,
                158=>'=DE' /*Þ*/, 159=>'=DF' /*ß*/, 160=>'=E0' /*à*/, 161=>'=E1' /*á*/, 162=>'=E2' /*â*/,
                163=>'=E3' /*ã*/, 164=>'=E4' /*ä*/, 165=>'=E5' /*å*/, 166=>'=E6' /*æ*/, 167=>'=E7' /*ç*/,
                168=>'=E8' /*è*/, 169=>'=E9' /*é*/, 170=>'=EA' /*ê*/, 171=>'=EB' /*ë*/, 172=>'=EC' /*ì*/,
                173=>'=ED' /*í*/, 174=>'=EE' /*î*/, 175=>'=EF' /*ï*/, 176=>'=F0' /*ð*/, 177=>'=F1' /*ñ*/,
                178=>'=F2' /*ò*/, 179=>'=F3' /*ó*/, 180=>'=F4' /*ô*/, 181=>'=F5' /*õ*/, 182=>'=F6' /*ö*/,
                183=>'=F7' /*÷*/, 184=>'=F8' /*ø*/, 185=>'=F9' /*ù*/, 186=>'=FA' /*ú*/, 187=>'=FB' /*û*/,
                188=>'=FC' /*ü*/, 189=>'=FD' /*ý*/, 190=>'=FE' /*þ*/, 191=>'=FF' /*ÿ*/,
            ],
            197 => [ // C5
                169=>'u' /*ũ*/, 168=>'U' /*Ũ*/,
            ]
        ];

        $textPre = str_replace("=", "=3D", $text);
        $result = FromUTF8::baseConversion($ISO88591_CONV, $textPre);
        if ($result == $textPre) {
            return $text;
        } else {
            $result = str_replace(" ", "_", $result);
        }

        if ($wrap == 0) {
            return "=?iso-8859-1?Q?".$result."?=";
        } else {
            $newResult = "=?iso-8859-1?Q?";
            $contaLinha = 0;
            $lenResult = strlen($result);
            for ($i = 0; $i < $lenResult; $i++) {
                if (($result[$i] == "=") && ($contaLinha >= ($wrap - 3)) || ($contaLinha >= $wrap)) {
                    $newResult .= "?=\r\n=?iso-8859-1?Q?".$result[$i];
                    $contaLinha = 0;
                } else {
                    $newResult .= $result[$i];
                }


                $contaLinha++;
            }
            $newResult .= "?=";
            return $newResult;
        }
    }
151642
public static function strtolower($text, $charset='utf-8') {
        $charset = self::parse_charset($charset);

        if ($charset === 'utf-8' and function_exists('mb_strtolower')) {
            return mb_strtolower($text, 'UTF-8');
        }

        $oldlevel = error_reporting(E_PARSE);
        $result = self::typo3()->conv_case($charset, (string)$text, 'toLower');
        error_reporting($oldlevel);

        return $result;
    }
855826
public static function strlen($text, $charset='utf-8') {
        $charset = self::parse_charset($charset);

        if ($charset === 'utf-8') {
            if (function_exists('mb_strlen')) {
                return mb_strlen($text, 'UTF-8');
            } else {
                return iconv_strlen($text, 'UTF-8');
            }
        }

        $oldlevel = error_reporting(E_PARSE);
        $result = self::typo3()->strlen($charset, (string)$text);
        error_reporting($oldlevel);

        return $result;
    }
1003401
def encode(cls, line):
        """"""
        if not line.encoded:
            encoding = getattr(line, 'encoding_param', None)
            if encoding and encoding.upper() == cls.base64string:
                line.value = b64encode(line.value).decode('utf-8')
            else:
                line.value = backslashEscape(str_(line.value))
            line.encoded = True
343037
function WriteText($x, $y, $txt)
	{
		// Output a string using Text() but does encoding and text reversing of RTL
		$txt = $this->purify_utf8_text($txt);
		if ($this->text_input_as_HTML) {
			$txt = $this->all_entities_to_utf8($txt);
		}
		if ($this->usingCoreFont) {
			$txt = mb_convert_encoding($txt, $this->mb_enc, 'UTF-8');
		}

		// DIRECTIONALITY
		if (preg_match("/([" . $this->pregRTLchars . "])/u", $txt)) {
			$this->biDirectional = true;
		} // *OTL*

		$textvar = 0;
		$save_OTLtags = $this->OTLtags;
		$this->OTLtags = [];
		if ($this->useKerning) {
			if ($this->CurrentFont['haskernGPOS']) {
				$this->OTLtags['Plus'] .= ' kern';
			} else {
				$textvar = ($textvar | TextVars::FC_KERNING);
			}
		}

		/* -- OTL -- */
		// Use OTL OpenType Table Layout - GSUB & GPOS
		if (isset($this->CurrentFont['useOTL']) && $this->CurrentFont['useOTL']) {
			$txt = $this->otl->applyOTL($txt, $this->CurrentFont['useOTL']);
			$OTLdata = $this->otl->OTLdata;
		}
		/* -- END OTL -- */
		$this->OTLtags = $save_OTLtags;

		$this->magic_reverse_dir($txt, $this->directionality, $OTLdata);

		$this->Text($x, $y, $txt, $OTLdata, $textvar);
	}
1314624
public function encodeQ($str, $position = 'text')
    {
        // There should not be any EOL in the string
        $pattern = '';
        $encoded = str_replace(array("\r", "\n"), '', $str);
        switch (strtolower($position)) {
            case 'phrase':
                // RFC 2047 section 5.3
                $pattern = '^A-Za-z0-9!*+\/ -';
                break;
            /** @noinspection PhpMissingBreakStatementInspection */
            case 'comment':
                // RFC 2047 section 5.2
                $pattern = '\(\)"';
                // intentional fall-through
                // for this reason we build the $pattern without including delimiters and []
            case 'text':
            default:
                // RFC 2047 section 5.1
                // Replace every high ascii, control, =, ? and _ characters
                $pattern = '\000-\011\013\014\016-\037\075\077\137\177-\377' . $pattern;
                break;
        }
        $matches = array();
        if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) {
            // If the string contains an '=', make sure it's the first thing we replace
            // so as to avoid double-encoding
            $eqkey = array_search('=', $matches[0]);
            if ($eqkey !== false) {
                unset($matches[0][$eqkey]);
                array_unshift($matches[0], '=');
            }
            foreach (array_unique($matches[0]) as $char) {
                $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded);
            }
        }
        // Replace every spaces to _ (more readable than =20)
        return str_replace(' ', '_', $encoded);
    }
979830
public function EncodeQ($str, $position = 'text')
    {
        // There should not be any EOL in the string
        $encoded = preg_replace('/[\r\n]*/', '', $str);

        switch (strtolower($position)) {
            case 'phrase':
                $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
                break;
            case 'comment':
                $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
            case 'text':
            default:
                // Replace every high ascii, control =, ? and _ characters
                //TODO using /e (equivalent to eval()) is probably not a good idea
                $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
                    "'='.sprintf('%02X', ord(stripslashes('\\1')))", $encoded);
                break;
        }

        // Replace every spaces to _ (more readable than =20)
        $encoded = str_replace(' ', '_', $encoded);

        return $encoded;
    }
542791
public static function lines($text, $start, $end)
	{
		$lines      = explode(PHP_EOL, $text);
		$code       = implode(PHP_EOL, array_slice($lines, $start - 1, $end - $start + 1));
		return $code;
	}
12834
public static function specialtoascii($text, $charset='utf-8') {
        $charset = self::parse_charset($charset);
        $oldlevel = error_reporting(E_PARSE);
        $result = self::typo3()->specCharsToASCII($charset, (string)$text);
        error_reporting($oldlevel);
        return $result;
    }
1188064
private static function utf8_encode(&$mixed)
    {
        if (is_array($mixed) || is_object($mixed)) {
            foreach ($mixed as &$value) {
                self::utf8_encode($value);
            }
        } else {
            if (is_string($mixed)) {
                $mixed = utf8_encode($mixed);
            }
        }
    }
1321135
public static function encode(
    /*string*/ $string,
    /*boolean*/ $oneLine = true
  ) {
    if(!self::isEncoded($string)) {
      $string = base64_encode($string);
    }
    if($string && $oneLine) {
      $string = str_replace(["\r", "\n"], '', $string);
    }
    return $string;
  }
435795
def wrapwrite(text):
    
    try:  # Python3
        sys.stdout.buffer.write(text)
    except AttributeError:
        sys.stdout.write(text.encode('utf-8'))
1183414
function __encode_utf8(text) {

        // Fill buffer with UTF-8
        for (var i=0; i<text.length; i++) {
            var codepoint = text.charCodeAt(i);
            __append_utf8(codepoint);
        }

        // Flush buffer
        if (length > 0) {
            var out_buffer = buffer.subarray(0, length);
            length = 0;
            return out_buffer;
        }

    }
1257583
public static function wrap($text, $left_margin = 0, $right_margin = 0,
        $width = null)
    {
        if (empty($width)) {
            $width = self::getWidth();
        }
        $width = $width - abs($left_margin) - abs($right_margin);
        $margin = str_repeat(' ', $left_margin);
        return $margin . wordwrap($text, $width, PHP_EOL . $margin);
    }