Returns a "safe" version of the given string - basically only US-ASCII and
numbers. Needed because filenames and titles and such, can't use all characters.

@param string $str
@param bool   $strict
@param string $extrachars

@return string
2802
public static function makeSafe($str, $strict = false, $extrachars = '')
    {
        $str = str_replace('&amp;', '', $str);

        $delim = '/';
        if ($extrachars != '') {
            $extrachars = preg_quote($extrachars, $delim);
        }
        if ($strict) {
            $slugify = Slugify::create([
                'regexp' => '/[^a-z0-9_' . $extrachars . ' -]+/'
            ]);
            $str = $slugify->slugify($str, '');
            $str = str_replace(' ', '-', $str);
        } else {
            // Allow Uppercase and don't convert spaces to dashes
            $slugify = Slugify::create([
                'regexp' => '/[^a-zA-Z0-9_.,' . $extrachars . ' -]+/',
                'lowercase' => false
            ]);
            $str = $slugify->slugify($str, '');
        }

        return $str;
    }
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;
    }
966657
public static function strToHex($str)
	{
		$r = "";
		$l = strlen($str);
		for($i = 0; $i < $l; $i++)
			$r .= str_pad(strtoupper(dechex(ord($str[$i]))), 2, " ",
				STR_PAD_LEFT) . " ";
		return $r;
	}
1080309
public static function ucwords(&$str, bool $strict=false) {
		if (\is_array($str)) {
			foreach ($str as $k=>$v) {
				static::ucwords($str[$k], $strict);
			}
		}
		elseif (! $strict || \is_string($str)) {
			cast::string($str, true);

			if ($str) {
				// The first letter.
				$str = \preg_replace_callback('/^(\p{L})/u', function($matches) {
					static::strtoupper($matches[0]);
					return $matches[0];
				}, $str);

				// Any letter following a dash, space, or forward slash.
				$str = \preg_replace_callback('/(\s|\p{Pd}|\/)(.)/u', function($matches) {
					static::strtoupper($matches[2], false);
					return $matches[1] . $matches[2];
				}, $str);
			}
		}
	}
1072216
public function toBin(string $str):string {
		$this->checkCharacterSet($str);

		// Convert the base32 string back to a binary string
		$str = array_map(function($chr){
			return sprintf('%08b', strpos($this->charset, $chr));
		}, str_split($str));

		// Remove the extra 0's we added
		$str = preg_replace('/000(.{5})/', '$1', implode('', $str));

		// Unpad if nessicary
		$length = strlen($str);
		$rbits = $length&7;

		if($rbits > 0){
			$str = substr($str, 0, $length-$rbits);
		}

		return $str;
	}
1303472
protected function sanitizeComponent($str)
    {
        if (is_null($str)) {
            return $str;
        }
        $str = filter_var((string) $str, FILTER_UNSAFE_RAW, array('flags' => FILTER_FLAG_STRIP_LOW));
        $str = trim($str);

        return $str;
    }
1333308
function encodeToUTF8($str)
    {
        $encodage = mb_detect_encoding($str, "UTF-8, ISO-8859-1, ISO-8859-15, windows-1252", true);
        $str_utf8 = mb_convert_encoding($str, "UTF-8", $encodage);
        return $str_utf8;
    }
562273
static function is_utf8($str)
    {
        $c=0; $b=0;
        $bits=0;
        $len=strlen($str);
        for($i=0; $i<$len; $i++)
        {
            $c=ord($str[$i]);
            if($c > 128)
            {
                if(($c >= 254)) return false;
                elseif($c >= 252) $bits=6;
                elseif($c >= 248) $bits=5;
                elseif($c >= 240) $bits=4;
                elseif($c >= 224) $bits=3;
                elseif($c >= 192) $bits=2;
                else return false;
                if(($i+$bits) > $len) return false;
                while($bits > 1)
                {
                    $i++;
                    $b=ord($str[$i]);
                    if($b < 128 || $b > 191) return false;
                    $bits--;
                }
            }
        }
        return true;
    }
50309
public function entitiesToAscii($str, $all = true) {
		if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
			for ($i = 0, $s = count($matches['0']); $i < $s; $i++) {
				$digits = $matches['1'][$i];

				$out = '';

				if ($digits < 128) {
					$out .= chr($digits);
				} elseif ($digits < 2048) {
					$out .= chr(192 + (($digits - ($digits % 64)) / 64));
					$out .= chr(128 + ($digits % 64));
				} else {
					$out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
					$out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
					$out .= chr(128 + ($digits % 64));
				}

				$str = str_replace($matches['0'][$i], $out, $str);
			}
		}

		if ($all) {
			$str = str_replace(['&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'],
				['&', '<', '>', '"', "'", '-'], $str);
		}

		return $str;
	}
86490
public function escape($text, $extra = false)
    {
        if (is_int($text) || is_float($text)) {
            return $text;
        }

        $text = str_replace("'", "''", $text);

        return addcslashes($text, "\000\n\r\\\032");
    }
828492
public static function getString(&$str)
    {
        $p = mb_orig_strpos($str, "\x00");
        if ($p === false) {
            return '';
        }
        $r = mb_orig_substr($str, 0, $p);
        $str = mb_orig_substr($str, $p + 1);
        return $r;
    }
466060
public static function formataFone($str)
    {
        $str = preg_replace("/[^0-9]/", '', $str);
        if (!$str) {
            return '';
        }
        if ('0800' == substr($str, 0, 4)) {
            $srt = substr($str, -11, -7) . ' ' . substr($str, -7, -4) . '-' . substr($str, -4);
        } elseif (11 == strlen($str)) {
            $srt = '(' . substr($str, -11, -9) . ') ' . substr($str, -9, -4) . '-' . substr($str, -4);
        } else {
            $srt = '(' . substr($str, -10, -8) . ') ' . substr($str, -8, -4) . '-' . substr($str, -4);
        }

        return $srt;
    }
1139605
function UTF8toSubset($str)
	{
		$ret = '<';
		// $str = preg_replace('/'.preg_quote($this->aliasNbPg,'/').'/', chr(7), $str );	// mPDF 6 deleted
		// $str = preg_replace('/'.preg_quote($this->aliasNbPgGp,'/').'/', chr(8), $str );	// mPDF 6 deleted
		$unicode = $this->UTF8StringToArray($str);
		$orig_fid = $this->CurrentFont['subsetfontids'][0];
		$last_fid = $this->CurrentFont['subsetfontids'][0];
		foreach ($unicode as $c) {
			/* 	// mPDF 6 deleted
			  if ($c == 7 || $c == 8) {
			  if ($orig_fid != $last_fid) {
			  $ret .= '> Tj /F'.$orig_fid.' '.$this->FontSizePt.' Tf <';
			  $last_fid = $orig_fid;
			  }
			  if ($c == 7) { $ret .= $this->aliasNbPgHex; }
			  else { $ret .= $this->aliasNbPgGpHex; }
			  continue;
			  }
			 */
			if (!$this->_charDefined($this->CurrentFont['cw'], $c)) {
				$c = 0;
			} // mPDF 6
			for ($i = 0; $i < 99; $i++) {
				// return c as decimal char
				$init = array_search($c, $this->CurrentFont['subsets'][$i]);
				if ($init !== false) {
					if ($this->CurrentFont['subsetfontids'][$i] != $last_fid) {
						$ret .= '> Tj /F' . $this->CurrentFont['subsetfontids'][$i] . ' ' . $this->FontSizePt . ' Tf <';
						$last_fid = $this->CurrentFont['subsetfontids'][$i];
					}
					$ret .= sprintf("%02s", strtoupper(dechex($init)));
					break;
				} // TrueType embedded SUBSETS
				elseif (count($this->CurrentFont['subsets'][$i]) < 255) {
					$n = count($this->CurrentFont['subsets'][$i]);
					$this->CurrentFont['subsets'][$i][$n] = $c;
					if ($this->CurrentFont['subsetfontids'][$i] != $last_fid) {
						$ret .= '> Tj /F' . $this->CurrentFont['subsetfontids'][$i] . ' ' . $this->FontSizePt . ' Tf <';
						$last_fid = $this->CurrentFont['subsetfontids'][$i];
					}
					$ret .= sprintf("%02s", strtoupper(dechex($n)));
					break;
				} elseif (!isset($this->CurrentFont['subsets'][($i + 1)])) {
					// TrueType embedded SUBSETS
					$this->CurrentFont['subsets'][($i + 1)] = [0 => 0];
					$new_fid = count($this->fonts) + $this->extraFontSubsets + 1;
					$this->CurrentFont['subsetfontids'][($i + 1)] = $new_fid;
					$this->extraFontSubsets++;
				}
			}
		}
		$ret .= '>';
		if ($last_fid != $orig_fid) {
			$ret .= ' Tj /F' . $orig_fid . ' ' . $this->FontSizePt . ' Tf <> ';
		}
		return $ret;
	}
487684
public function decode($str)
	{
		if ($this->decodeHtmlEntities && strpos($str, '&') !== false)
		{
			$str = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
		}
		$str = str_replace("\x1A", '', $str);

		if ($this->hasEscapedChars)
		{
			$str = strtr(
				$str,
				[
					"\x1B0" => '!', "\x1B1" => '"', "\x1B2" => "'", "\x1B3" => '(',
					"\x1B4" => ')', "\x1B5" => '*', "\x1B6" => '[', "\x1B7" => '\\',
					"\x1B8" => ']', "\x1B9" => '^', "\x1BA" => '_', "\x1BB" => '`',
					"\x1BC" => '~'
				]
			);
		}

		return $str;
	}
430823
public function ascii($str, $allow_non_ascii = false)
	{
		// Translate unicode characters to their simpler counterparts
		$this->config->load('ascii', true);
		$foreign_characters = $this->config->get('ascii');

		$str = preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);

		if ( ! $allow_non_ascii)
		{
			return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $str);
		}

		return $str;
	}
1246793
public static function xmlEscape($str)
    {
        // array used to figure what number to decrement from character order value
        // according to number of characters used to map unicode to ascii by utf-8
        $decrement = [];
        $decrement[4] = 240;
        $decrement[3] = 224;
        $decrement[2] = 192;
        $decrement[1] = 0;

        // the number of bits to shift each charNum by
        $shift = [];
        $shift[1][0] = 0;
        $shift[2][0] = 6;
        $shift[2][1] = 0;
        $shift[3][0] = 12;
        $shift[3][1] = 6;
        $shift[3][2] = 0;
        $shift[4][0] = 18;
        $shift[4][1] = 12;
        $shift[4][2] = 6;
        $shift[4][3] = 0;

        $pos = 0;
        // using standard strlen and letting loop determine # of bytes-per-char
        $len = strlen($str);
        $xml = '';

        while ($pos < $len) {
            $asciiPos = ord(substr($str, $pos, 1));
            if (($asciiPos >= 240) && ($asciiPos <= 255)) {
                // 4 chars representing one unicode character
                $thisLetter = substr($str, $pos, 4);
                $pos += 4;
            } else if (($asciiPos >= 224) && ($asciiPos <= 239)) {
                // 3 chars representing one unicode character
                $thisLetter = substr($str, $pos, 3);
                $pos += 3;
            } else if (($asciiPos >= 192) && ($asciiPos <= 223)) {
                // 2 chars representing one unicode character
                $thisLetter = substr($str, $pos, 2);
                $pos += 2;
            } else {
                // 1 char (lower ascii)
                $thisLetter = substr($str, $pos, 1);
                $pos += 1;
            }

            // process the string representing the letter to a unicode entity
            $thisLen = strlen($thisLetter);
            $thisPos = 0;
            $decimalCode = 0;
            while ($thisPos < $thisLen) {
                $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
                if ($thisPos == 0) {
                    $charNum = intval($thisCharOrd - $decrement[$thisLen]);
                    $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
                } else {
                    $charNum = intval($thisCharOrd - 128);
                    $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
                }

                $thisPos++;
            }

            if ($thisLen == 1) {
                $encodedLetter = '&#' . str_pad($decimalCode, 3, '0', STR_PAD_LEFT) . ';';
            } else {
                $encodedLetter = '&#' . str_pad($decimalCode, 5, '0', STR_PAD_LEFT) . ';';
            }

            $c = $decimalCode;


            if ($c > 0 && $c < 32) {
                $xml .= $encodedLetter;
            } else if ($c >= 32 && $c < 127) {
                switch ($thisLetter) {
                    case '<':
                        $xml .= '&lt;';
                        break;
                    case '>':
                        $xml .= '&gt;';
                        break;
                    case '&':
                        $xml .= '&amp;';
                        break;
                    case '"':
                        $xml .= '&quot;';
                        break;
                    default:
                        $xml .= $thisLetter;
                }
            } else {
                $xml .= $encodedLetter;
            }
        }

        // final mutant possibility (the amped amp)
        $xml = str_replace('&amp;amp;', '&amp;', $xml);
        return trim($xml);
    }
171126
public static function encode(String $str) : String
    {
        $chars = self::$numericalCodes;

        return str_replace(array_keys($chars), array_values($chars), $str);
    }