Encode PHP Tag

@param string $str

@return string
199318
public static function encode(String $str) : String
    {
        return str_replace(array_keys(self::$phpTagChars), array_values(self::$phpTagChars), $str);
    }
467593
public static function replaceRoman(string $str) : string
    {
        $str = self::strToArray($str);
        return implode('', self::replaceRomanArray($str));
    }
944551
public function encodeHeader($str, $position = 'text')
    {
        $matchcount = 0;
        switch (strtolower($position)) {
            case 'phrase':
                if (!preg_match('/[\200-\377]/', $str)) {
                    // Can't use addslashes as we don't know the value of magic_quotes_sybase
                    $encoded = addcslashes($str, "\0..\37\177\\\"");
                    if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
                        return ($encoded);
                    } else {
                        return ("\"$encoded\"");
                    }
                }
                $matchcount = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
                break;
            /** @noinspection PhpMissingBreakStatementInspection */
            case 'comment':
                $matchcount = preg_match_all('/[()"]/', $str, $matches);
            // Intentional fall-through
            case 'text':
            default:
                $matchcount += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
                break;
        }

        if ($matchcount == 0) { // There are no chars that need encoding
            return ($str);
        }

        $maxlen = 75 - 7 - strlen($this->CharSet);
        // Try to select the encoding which should produce the shortest output
        if ($matchcount > strlen($str) / 3) {
            // More than a third of the content will need encoding, so B encoding will be most efficient
            $encoding = 'B';
            if (function_exists('mb_strlen') && $this->hasMultiBytes($str)) {
                // Use a custom function which correctly encodes and wraps long
                // multibyte strings without breaking lines within a character
                $encoded = $this->base64EncodeWrapMB($str, "\n");
            } else {
                $encoded = base64_encode($str);
                $maxlen -= $maxlen % 4;
                $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
            }
        } else {
            $encoding = 'Q';
            $encoded = $this->encodeQ($str, $position);
            $encoded = $this->wrapText($encoded, $maxlen, true);
            $encoded = str_replace('=' . self::CRLF, "\n", trim($encoded));
        }

        $encoded = preg_replace('/^(.*)$/m', ' =?' . $this->CharSet . "?$encoding?\\1?=", $encoded);
        $encoded = trim(str_replace("\n", $this->LE, $encoded));

        return $encoded;
    }
532941
public function fromBin(string $str):string {
		$this->checkLength($str);
		$this->checkBin($str);

		// Base32 works on the first 5 bits of a byte, so we insert blanks to pad it out
		$str = preg_replace('/(.{5})/', '000$1', $str);

		// We need a string divisible by 5
		$length = strlen($str);
		$rbits = $length&7;

		if($rbits > 0){
			// Excessive bits need to be padded
			$ebits = substr($str, $length-$rbits);
			$str = substr($str, 0, $length-$rbits).'000'.$ebits.str_repeat('0', 5-strlen($ebits));
		}

		preg_match_all('/.{8}/', $str, $chrs);

		$chrs = array_map(function($str){
			return $this->charset[bindec($str)];
		}, $chrs[0]);

		return implode('', $chrs);
	}
1278134
function encode(str) {
  var result = '',
      bytes = utf8Encode(str),
      length = bytes.length,
      i;

  // Convert every three bytes to 4 ascii characters.
  for (i = 0; i < (length - 2); i += 3) {
    result += chrTable[bytes[i] >> 2];
    result += chrTable[((bytes[i] & 0x03) << 4) + (bytes[i + 1] >> 4)];
    result += chrTable[((bytes[i + 1] & 0x0f) << 2) + (bytes[i + 2] >> 6)];
    result += chrTable[bytes[i + 2] & 0x3f];
  }

  // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
  if (length % 3) {
    i = length - (length % 3);
    result += chrTable[bytes[i] >> 2];
    if ((length % 3) === 2) {
      result += chrTable[((bytes[i] & 0x03) << 4) + (bytes[i + 1] >> 4)];
      result += chrTable[(bytes[i + 1] & 0x0f) << 2];
      result += padding;
    } else {
      result += chrTable[(bytes[i] & 0x03) << 4];
      result += padding + padding;
    }
  }

  return result;
}
635070
public static function decode(String $str) : String
    {
        return preg_replace
        (
            array_keys(self::$scriptTagCharsDecode), 
            array_values(self::$scriptTagCharsDecode), 
            $str
        );
    }
1344665
public static function stringFromDB2HTML($str)
    {

        if (!$str || $str === null || $str === '' || is_array($str)) {
            return $str;
        }

        $str = trim($str);
        $str = htmlspecialchars($str, ENT_COMPAT, 'utf-8');

        return $str;
    }
626318
public function toString($str = '')
    {
        $str .= "<{$this->name} xmlns='{$this->ns}' ";
        foreach ($this->attrs as $key => $value) {
            if ($key !== 'xmlns') {
                $value = htmlspecialchars($value);
                $str .= "$key='$value' ";
            }
        }
        $str .= ">";
        foreach ($this->subs as $sub) {
            $str .= $sub->toString();
        }
        $body = htmlspecialchars($this->data);
        $str .= "$body</{$this->name}>";
        return $str;
    }
1308181
public static function convertEncoding( $str, $html_charset, $to_encoding = 'UTF-8' )
    {
        if (empty($html_charset)){
            return $str;
        }

        $php_encoding = self::getPhpEncoding($html_charset);
        $from_encoding = $php_encoding ? $php_encoding : 'auto';

        $str = ( $from_encoding == $to_encoding ) ? $str : mb_convert_encoding( $str, $to_encoding, $from_encoding );

        return $str;
    }
143366
public static function escapeString(string $str): string
    {
        // one of the characters ",", "+", """, "\", "<", ">" or ";"
        $str = preg_replace('/([,\+"\\\<\>;])/u', '\\\\$1', $str);
        // a space character occurring at the end of the string
        $str = preg_replace('/( )$/u', '\\\\$1', $str);
        // a space or "#" character occurring at the beginning of the string
        $str = preg_replace('/^([ #])/u', '\\\\$1', $str);
        // implementation specific special characters
        $str = preg_replace_callback('/([\pC])/u',
            function ($m) {
                $octets = str_split(bin2hex($m[1]), 2);
                return implode("",
                    array_map(
                        function ($octet) {
                            return '\\' . strtoupper($octet);
                        }, $octets));
            }, $str);
        return $str;
    }
740562
public static function toXmlCompliant($str, $striptags = true)
    {
        if (true === $striptags) {
            $str = strip_tags($str);
        }
        $str = html_entity_decode($str, ENT_COMPAT, 'UTF-8');
        $str = str_replace(array('<', '>', '&'), array('&lt;', '&gt;', '&amp;'), $str);

        return $str;
    }
1338364
private function generateAppletTag($str) {
	$this->logDebug('generateAppletTag', 'Entering function');
	$str = preg_replace('/'.$this->classparams['tag_jscript'].'/', $this->str_jsinit(), $str);
	return preg_replace('/'.$this->classparams['tag_applet'].'/', $this->str_applet(), $str);
}
138680
public function toString(string $str):string {
		$str = strtoupper($str);

		// csSave actually has to be able to consider extra characters
		if($this->charset === $this::CROCKFORD){
			$str = str_replace('O', '0', $str);
			$str = str_replace(['I', 'L'], '1', $str);
		}

		return $this->bin2str($this->toBin($str));
	}
280830
public static function encodingWithUtf8($str)
    {
        $cur_encoding = mb_detect_encoding($str);
        if ($cur_encoding == "UTF-8" && mb_check_encoding($str, "UTF-8")) {
            return $str;
        } else {
            return utf8_encode($str);
        }
    }
1325494
public static function markString($str): string
    {
        $str_len = strlen($str);
        $start = 6 < $str_len ? intval($str_len / 5) : $str_len - 4;
        $end = $str_len - $start * 3;
        
        return preg_replace('|(?<=.{' . $start . '})(.{' . $end . '}).*?|', str_pad('', $end, '*'), $str, 1);
    }
320381
public static String encode(String str)
    {
        String encoded;
        try {
            encoded = URLEncoder.encode(str, "UTF-8").replace(".", "%2E").replace("*", "%2A");
        } catch (UnsupportedEncodingException e) {
            // Should never happen

            encoded = str;
        }

        return encoded;
    }
131030
public function encodeHex($str)
    {
        if (!ctype_xdigit((string)$str)) {
            return '';
        }

        $numbers = trim(chunk_split($str, 12, ' '));
        $numbers = explode(' ', $numbers);

        foreach ($numbers as $i => $number) {
            $numbers[$i] = hexdec('1' . $number);
        }

        return call_user_func_array([$this, 'encode'], $numbers);
    }