\\s*?<\\/[^>]*?>/u',
'',
$str
);
}
/**
* Convert all applicable characters to HTML entities: UTF-8 version of htmlentities().
*
* EXAMPLE: UTF8::htmlentities('<白-öäü>'); // '<白-öäü>'
*
* @see http://php.net/manual/en/function.htmlentities.php
*
* @param string $str
* The input string. *
* @param int $flags [optional]* A bitmask of one or more of the following flags, which specify how to handle * quotes, invalid code unit sequences and the used document type. The default is * ENT_COMPAT | ENT_HTML401. *
| Constant Name | *Description | *
| ENT_COMPAT | *Will convert double-quotes and leave single-quotes alone. | *
| ENT_QUOTES | *Will convert both double and single quotes. | *
| ENT_NOQUOTES | *Will leave both double and single quotes unconverted. | *
| ENT_IGNORE | ** Silently discard invalid code unit sequences instead of returning * an empty string. Using this flag is discouraged as it * may have security implications. * | *
| ENT_SUBSTITUTE | ** Replace invalid code unit sequences with a Unicode Replacement Character * U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty * string. * | *
| ENT_DISALLOWED | ** Replace invalid code points for the given document type with a * Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; * (otherwise) instead of leaving them as is. This may be useful, for * instance, to ensure the well-formedness of XML documents with * embedded external content. * | *
| ENT_HTML401 | ** Handle code as HTML 4.01. * | *
| ENT_XML1 | ** Handle code as XML 1. * | *
| ENT_XHTML | ** Handle code as XHTML. * | *
| ENT_HTML5 | ** Handle code as HTML 5. * | *
* Like htmlspecialchars, * htmlentities takes an optional third argument * encoding which defines encoding used in * conversion. * Although this argument is technically optional, you are highly * encouraged to specify the correct value for your code. *
* @param bool $double_encode [optional]* When double_encode is turned off PHP will not * encode existing html entities. The default is to convert everything. *
* * @psalm-pure * * @return string *
* The encoded string.
*
* If the input string contains an invalid code unit
* sequence within the given encoding an empty string
* will be returned, unless either the ENT_IGNORE or
* ENT_SUBSTITUTE flags are set.
*
UTF8::htmlspecialchars('<白-öäü>'); // '<白-öäü>'
*
* @see http://php.net/manual/en/function.htmlspecialchars.php
*
* @param string $str * The string being converted. *
* @param int $flags [optional]* A bitmask of one or more of the following flags, which specify how to handle * quotes, invalid code unit sequences and the used document type. The default is * ENT_COMPAT | ENT_HTML401. *
| Constant Name | *Description | *
| ENT_COMPAT | *Will convert double-quotes and leave single-quotes alone. | *
| ENT_QUOTES | *Will convert both double and single quotes. | *
| ENT_NOQUOTES | *Will leave both double and single quotes unconverted. | *
| ENT_IGNORE | ** Silently discard invalid code unit sequences instead of returning * an empty string. Using this flag is discouraged as it * may have security implications. * | *
| ENT_SUBSTITUTE | ** Replace invalid code unit sequences with a Unicode Replacement Character * U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty * string. * | *
| ENT_DISALLOWED | ** Replace invalid code points for the given document type with a * Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; * (otherwise) instead of leaving them as is. This may be useful, for * instance, to ensure the well-formedness of XML documents with * embedded external content. * | *
| ENT_HTML401 | ** Handle code as HTML 4.01. * | *
| ENT_XML1 | ** Handle code as XML 1. * | *
| ENT_XHTML | ** Handle code as XHTML. * | *
| ENT_HTML5 | ** Handle code as HTML 5. * | *
* Defines encoding used in conversion. *
** For the purposes of this function, the encodings * ISO-8859-1, ISO-8859-15, * UTF-8, cp866, * cp1251, cp1252, and * KOI8-R are effectively equivalent, provided the * string itself is valid for the encoding, as * the characters affected by htmlspecialchars occupy * the same positions in all of these encodings. *
* @param bool $double_encode [optional]* When double_encode is turned off PHP will not * encode existing html entities, the default is to convert everything. *
* * @psalm-pure * * @return string *The converted string.
** If the input string contains an invalid code unit * sequence within the given encoding an empty string * will be returned, unless either the ENT_IGNORE or * ENT_SUBSTITUTE flags are set.
* * @template T as string * @phpstan-param T $str * @phpstan-return (T is non-empty-string ? non-empty-string : string) */ public static function htmlspecialchars( string $str, int $flags = \ENT_COMPAT, string $encoding = 'UTF-8', bool $double_encode = true ): string { if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { $encoding = self::normalize_encoding($encoding, 'UTF-8'); } return \htmlspecialchars( $str, $flags, $encoding, $double_encode ); } /** * Checks whether iconv is available on the server. * * @psalm-pure * * @return bool *true if available, false otherwise
* * @internalPlease do not use it anymore, we will make is private in next major version.
*/ public static function iconv_loaded(): bool { return \extension_loaded('iconv'); } /** * Converts Integer to hexadecimal U+xxxx code point representation. * * INFO: opposite to UTF8::hex_to_int() * * EXAMPLE:UTF8::int_to_hex(241); // 'U+00f1'
*
* @param int $int The integer to be converted to hexadecimal code point.
* @param string $prefix [optional] * * @psalm-pure * * @return string the code point, or empty string on failure */ public static function int_to_hex(int $int, string $prefix = 'U+'): string { $hex = \dechex($int); $hex = (\strlen($hex) < 4 ? \substr('0000' . $hex, -4) : $hex); return $prefix . $hex . ''; } /** * Checks whether intl-char is available on the server. * * @psalm-pure * * @return bool *true if available, false otherwise
* * @internalPlease do not use it anymore, we will make is private in next major version.
*/ public static function intlChar_loaded(): bool { return \class_exists('IntlChar'); } /** * Checks whether intl is available on the server. * * @psalm-pure * * @return bool *true if available, false otherwise
* * @internalPlease do not use it anymore, we will make is private in next major version.
*/ public static function intl_loaded(): bool { return \extension_loaded('intl'); } /** * Returns true if the string contains only alphabetic chars, false otherwise. * * @param string $strThe input string.
* * @psalm-pure * * @return bool *Whether or not $str contains only alphabetic chars.
*/ public static function is_alpha(string $str): bool { if (self::$SUPPORT['mbstring'] === true) { return \mb_ereg_match('^[[:alpha:]]*$', $str); } return self::str_matches_pattern($str, '^[[:alpha:]]*$'); } /** * Returns true if the string contains only alphabetic and numeric chars, false otherwise. * * @param string $strThe input string.
* * @psalm-pure * * @return bool *Whether or not $str contains only alphanumeric chars.
*/ public static function is_alphanumeric(string $str): bool { if (self::$SUPPORT['mbstring'] === true) { return \mb_ereg_match('^[[:alnum:]]*$', $str); } return self::str_matches_pattern($str, '^[[:alnum:]]*$'); } /** * Returns true if the string contains only punctuation chars, false otherwise. * * @param string $strThe input string.
* * @psalm-pure * * @return bool *Whether or not $str contains only punctuation chars.
*/ public static function is_punctuation(string $str): bool { return self::str_matches_pattern($str, '^[[:punct:]]*$'); } /** * Returns true if the string contains only printable (non-invisible) chars, false otherwise. * * @param string $strThe input string.
* @param bool $ignore_control_characters [optional]Ignore control characters like [LRM] or [LSEP].
* * @psalm-pure * * @return bool *Whether or not $str contains only printable (non-invisible) chars.
*/ public static function is_printable(string $str, bool $ignore_control_characters = false): bool { return self::remove_invisible_characters($str, false, '', $ignore_control_characters) === $str; } /** * Checks if a string is 7 bit ASCII. * * EXAMPLE:UTF8::is_ascii('白'); // false
*
* @param string $str The string to check.
* * @psalm-pure * * @return bool *
* true if it is ASCII
* false otherwise
*
UTF8::is_base64('4KSu4KWL4KSo4KS/4KSa'); // true
*
* @param string|null $str The input string.
* @param bool $empty_string_is_valid [optional]Is an empty string valid base64 or not?
* * @psalm-pure * * @return bool *Whether or not $str is base64 encoded.
*/ public static function is_base64($str, bool $empty_string_is_valid = false): bool { if ( !$empty_string_is_valid && $str === '' ) { return false; } if (!\is_string($str)) { return false; } $base64String = \base64_decode($str, true); return $base64String !== false && \base64_encode($base64String) === $str; } /** * Check if the input is binary... (is look like a hack). * * EXAMPLE:UTF8::is_binary(01); // true
*
* @param int|string $input
* @param bool $strict
*
* @psalm-pure
*
* @return bool
*/
public static function is_binary($input, bool $strict = false): bool
{
$input = (string) $input;
if ($input === '') {
return false;
}
if (\preg_match('~^[01]+$~', $input)) {
return true;
}
$ext = self::get_file_type($input);
if ($ext['type'] === 'binary') {
return true;
}
if (!$strict) {
$test_length = \strlen($input);
$test_null_counting = \substr_count($input, "\x0", 0, $test_length);
if (($test_null_counting / $test_length) > 0.25) {
return true;
}
}
if ($strict) {
if (self::$SUPPORT['finfo'] === false) {
throw new \RuntimeException('ext-fileinfo: is not installed');
}
/**
* @psalm-suppress ImpureMethodCall - it will return the same result for the same file ...
*/
$finfo_encoding = (new \finfo(\FILEINFO_MIME_ENCODING))->buffer($input);
if ($finfo_encoding && $finfo_encoding === 'binary') {
return true;
}
}
return false;
}
/**
* Check if the file is binary.
*
* EXAMPLE: UTF8::is_binary('./utf32.txt'); // true
*
* @param string $file
*
* @return bool
*/
public static function is_binary_file($file): bool
{
// init
$block = '';
$fp = \fopen($file, 'rb');
if (\is_resource($fp)) {
$block = \fread($fp, 512);
\fclose($fp);
}
if ($block === '' || $block === false) {
return false;
}
return self::is_binary($block, true);
}
/**
* Returns true if the string contains only whitespace chars, false otherwise.
*
* @param string $str The input string.
* * @psalm-pure * * @return bool *Whether or not $str contains only whitespace characters.
*/ public static function is_blank(string $str): bool { if (self::$SUPPORT['mbstring'] === true) { return \mb_ereg_match('^[[:space:]]*$', $str); } return self::str_matches_pattern($str, '^[[:space:]]*$'); } /** * Checks if the given string is equal to any "Byte Order Mark". * * WARNING: Use "UTF8::string_has_bom()" if you will check BOM in a string. * * EXAMPLE:UTF8::is_bom("\xef\xbb\xbf"); // true
*
* @param string $str The input string.
* * @psalm-pure * * @return bool *true if the $utf8_chr is Byte Order Mark, false otherwise.
*/ public static function is_bom($str): bool { /** @noinspection PhpUnusedLocalVariableInspection */ foreach (self::$BOM as $bom_string => &$bom_byte_length) { if ($str === $bom_string) { return true; } } return false; } /** * Determine whether the string is considered to be empty. * * A variable is considered empty if it does not exist or if its value equals FALSE. * empty() does not generate a warning if the variable does not exist. * * @param arrayWhether or not $str is empty().
*/ public static function is_empty($str): bool { return empty($str); } /** * Returns true if the string contains only hexadecimal chars, false otherwise. * * @param string $strThe input string.
* * @psalm-pure * * @return bool *Whether or not $str contains only hexadecimal chars.
*/ public static function is_hexadecimal(string $str): bool { if (self::$SUPPORT['mbstring'] === true) { return \mb_ereg_match('^[[:xdigit:]]*$', $str); } return self::str_matches_pattern($str, '^[[:xdigit:]]*$'); } /** * Check if the string contains any HTML tags. * * EXAMPLE:UTF8::is_html('lall'); // true
*
* @param string $str The input string.
* * @psalm-pure * * @return bool *Whether or not $str contains html elements.
*/ public static function is_html(string $str): bool { if ($str === '') { return false; } // init $matches = []; $str = self::emoji_encode($str); // hack for emoji support :/ \preg_match("/<\\/?\\w+(?:(?:\\s+\\w+(?:\\s*=\\s*(?:\".*?\"|'.*?'|[^'\">\\s]+))?)*\\s*|\\s*)\\/?>/u", $str, $matches); return $matches !== []; } /** * Check if $url is an correct url. * * @param string $url * @param bool $disallow_localhost * * @psalm-pure * * @return bool */ public static function is_url(string $url, bool $disallow_localhost = false): bool { if ($url === '') { return false; } // WARNING: keep this as hack protection if (!self::str_istarts_with_any($url, ['http://', 'https://'])) { return false; } // e.g. -> the server itself connect to "https://foo.localhost/phpmyadmin/... if ($disallow_localhost) { if (self::str_istarts_with_any( $url, [ 'http://localhost', 'https://localhost', 'http://127.0.0.1', 'https://127.0.0.1', 'http://::1', 'https://::1', ] )) { return false; } $regex = '/^(?:http(?:s)?:\/\/).*?(?:\.localhost)/iu'; if (\preg_match($regex, $url)) { return false; } } // INFO: this is needed for e.g. "http://müller.de/" (internationalized domain names) and non ASCII-parameters $regex = '/^(?:http(?:s)?:\\/\\/)(?:[\p{L}0-9][\p{L}0-9_-]*(?:\\.[\p{L}0-9][\p{L}0-9_-]*))(?:\\d+)?(?:\\/\\.*)?/iu'; if (\preg_match($regex, $url)) { return true; } return \filter_var($url, \FILTER_VALIDATE_URL) !== false; } /** * Try to check if "$str" is a JSON-string. * * EXAMPLE:UTF8::is_json('{"array":[1,"¥","ä"]}'); // true
*
* @param string $str The input string.
* @param bool $only_array_or_object_results_are_valid [optional]Only array and objects are valid json * results.
* * @return bool *Whether or not the $str is in JSON format.
*/ public static function is_json(string $str, bool $only_array_or_object_results_are_valid = true): bool { if ($str === '') { return false; } if (self::$SUPPORT['json'] === false) { throw new \RuntimeException('ext-json: is not installed'); } $jsonOrNull = self::json_decode($str); if ($jsonOrNull === null && \strtoupper($str) !== 'NULL') { return false; } if ( $only_array_or_object_results_are_valid && !\is_object($jsonOrNull) && !\is_array($jsonOrNull) ) { return false; } return \json_last_error() === \JSON_ERROR_NONE; } /** * @param string $strThe input string.
* * @psalm-pure * * @return bool *Whether or not $str contains only lowercase chars.
*/ public static function is_lowercase(string $str): bool { if (self::$SUPPORT['mbstring'] === true) { return \mb_ereg_match('^[[:lower:]]*$', $str); } return self::str_matches_pattern($str, '^[[:lower:]]*$'); }