Copied!

🇷🇺 Русским гражданам

В Украине сейчас идет война. Силами РФ наносятся удары по гражданской инфраструктуре в [Харькове][1], [Киеве][2], [Чернигове][3], [Сумах][4], [Ирпене][5] и десятках других городов. Гибнут люди - и гражданское население, и военные, в том числе российские призывники, которых бросили воевать. Чтобы лишить собственный народ доступа к информации, правительство РФ запретило называть войну войной, закрыло независимые СМИ и принимает сейчас ряд диктаторских законов. Эти законы призваны заткнуть рот всем, кто против войны. За обычный призыв к миру сейчас можно получить несколько лет тюрьмы.

Не молчите! Молчание - знак вашего согласия с политикой российского правительства. Вы можете сделать выбор НЕ МОЛЧАТЬ.

🇺🇸 To people of Russia

There is a war in Ukraine right now. The forces of the Russian Federation are attacking civilian infrastructure in [Kharkiv][1], [Kyiv][2], [Chernihiv][3], [Sumy][4], [Irpin][5] and dozens of other cities. People are dying – both civilians and military servicemen, including Russian conscripts who were thrown into the fighting. In order to deprive its own people of access to information, the government of the Russian Federation has forbidden calling a war a war, shut down independent media and is passing a number of dictatorial laws. These laws are meant to silence all those who are against war. You can be jailed for multiple years for simply calling for peace. Do not be silent! Silence is a sign that you accept the Russian government's policy. You can choose NOT TO BE SILENT.

  • immutable
CloneableFinalInstantiable
Methods
public __construct()
 

__construct()

public static access(string $str, int $pos, string $encoding = 'UTF-8') : string
 

Return the character at the specified position: $str[1] like functionality.

EXAMPLE: UTF8::access('fòô', 1); // 'ò'

  • param string $str
  • param int $pos
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static add_bom_to_string(string $str) : string
 

Prepends UTF-8 BOM character to the string and returns the whole string.

INFO: If BOM already existed there, the Input string is returned.

EXAMPLE: UTF8::add_bom_to_string('fòô'); // "\xEF\xBB\xBF" . 'fòô'

  • param string $str
  • psalm-pure
  • return non-empty-string
public static array_change_key_case(array $array, int $case = 0CASE_LOWER, string $encoding = 'UTF-8') : array
 

Changes all keys in an array.

  • param array<string,mixed> $array
  • param int $case [optional] Either CASE_UPPER or CASE_LOWER (default)
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string[]
public static between(string $str, string $start, string $end, int $offset = 0, string $encoding = 'UTF-8') : string
 

Returns the substring between $start and $end, if found, or an empty string. An optional offset may be supplied from which to begin the search for the start string.

  • param string $str
  • param string $start
  • param string $end
  • param int $offset [optional] Index from which to begin the search. Default: 0
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static binary_to_str( $bin) : string
 

Convert binary into a string.

INFO: opposite to UTF8::str_to_binary()

EXAMPLE: UTF8::binary_to_str('11110000100111111001100010000011'); // '😃'

  • param string $bin 1|0
  • psalm-pure
  • return string
public static bom() : string
 

Returns the UTF-8 Byte Order Mark Character.

INFO: take a look at UTF8::$bom for e.g. UTF-16 and UTF-32 BOM values

EXAMPLE: UTF8::bom(); // "\xEF\xBB\xBF"

  • psalm-pure
  • return non-empty-string
public static callback( $callback, string $str) : array
 
  • alias of UTF8::chr_map()
  • param callable string $callback
  • param string $str
  • psalm-pure
  • return string[]
  • see \UTF8::chr_map()
public static char_at(string $str, int $index, string $encoding = 'UTF-8') : string
 

Returns the character at $index, with indexes starting at 0.

  • param string $str
  • param int<1, max> $index
  • param string $encoding [optional] Default is UTF-8
  • psalm-pure
  • return string <p>The character at $index.
public static chars(string $str) : array
 

Returns an array consisting of the characters in the string.

  • param string $str
  • psalm-pure
  • return string[]
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-list : list)
public static checkForSupport()
 

This method will auto-detect your server environment for UTF-8 support.

  • return true|null
  • internal
public static chr( $code_point, string $encoding = 'UTF-8')
 

Generates a UTF-8 encoded character from the given code point.

INFO: opposite to UTF8::ord()

EXAMPLE: UTF8::chr(0x2603); // '☃'

  • param int $code_point
  • param string $encoding [optional] Default is UTF-8
  • psalm-pure
  • return string|null
public static chr_map( $callback, string $str) : array
 

Applies callback to all characters of a string.

EXAMPLE: UTF8::chr_map([UTF8::class, 'strtolower'], 'Κόσμε'); // ['κ','ό', 'σ', 'μ', 'ε']

  • param callable string $callback The callback function.
  • param string $str
  • psalm-pure
  • return string[]
public static chr_size_list(string $str) : array
 

Generates an array of byte length of each character of a Unicode string.

1 byte => U+0000 - U+007F 2 byte => U+0080 - U+07FF 3 byte => U+0800 - U+FFFF 4 byte => U+10000 - U+10FFFF

EXAMPLE: UTF8::chr_size_list('中文空白-test'); // [3, 3, 3, 3, 1, 1, 1, 1, 1]

  • param string $str
  • psalm-pure
  • return int[]
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-list<1|2|3|4> : list<1|2|3|4>)
public static chr_to_decimal(string $char) : int
 

Get a decimal code representation of a specific character.

INFO: opposite to UTF8::decimal_to_chr()

EXAMPLE: UTF8::chr_to_decimal('§'); // 0xa7

  • param string $char
  • psalm-pure
  • return int
public static chr_to_hex( $char, string $prefix = 'U+') : string
 

Get hexadecimal code point (U+xxxx) of a UTF-8 encoded character.

EXAMPLE: UTF8::chr_to_hex('§'); // U+00a7

  • param int|string $char
  • param string $prefix [optional]
  • psalm-pure
  • return string
public static chunk_split(string $str, int $chunk_length = 76, string $end = ' ') : string
 

Splits a string into smaller chunks and multiple lines, using the specified line ending character.

EXAMPLE: UTF8::chunk_split('ABC-ÖÄÜ-中文空白-κόσμε', 3); // "ABC\r\n-ÖÄ\r\nÜ-中\r\n文空白\r\n-κό\r\nσμε"

  • param string $str
  • param int<1, max> $chunk_length [optional] The maximum character length of a chunk.
  • param string $end [optional] The character(s) to be inserted at the end of each chunk.
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static clean(string $str, bool $remove_bom = false, bool $normalize_whitespace = false, bool $normalize_msword = false, bool $keep_non_breaking_space = false, bool $replace_diamond_question_mark = false, bool $remove_invisible_characters = true, bool $remove_invisible_characters_url_encoded = false) : string
 

Accepts a string and removes all non-UTF-8 characters from it + extras if needed.

EXAMPLE: UTF8::clean("\xEF\xBB\xBF„Abcdef\xc2\xa0\x20…” — 😃 - Düsseldorf", true, true); // '„Abcdef  …” — 😃 - Düsseldorf'

  • param string $str
  • param bool $remove_bom [optional] Set to true, if you need to remove UTF-BOM.
  • param bool $normalize_whitespace [optional] Set to true, if you need to normalize the whitespace.
  • param bool $normalize_msword [optional] Set to true, if you need to normalize MS Word chars e.g.: "…" => "..."
  • param bool $keep_non_breaking_space [optional] <p>Set to true, to keep non-breaking-spaces, in combination with $normalize_whitespace
  • param bool $replace_diamond_question_mark [optional] Set to true, if you need to remove diamond question mark e.g.: "�"
  • param bool $remove_invisible_characters [optional] Set to false, if you not want to remove invisible characters e.g.: "\0"
  • param bool $remove_invisible_characters_url_encoded [optional] Set to true, if you not want to remove invisible url encoded characters e.g.: "%0B" WARNING: maybe contains false-positives e.g. aa%0Baa -> aaaa.
  • psalm-pure
  • return string
public static cleanup( $str) : string
 

Clean-up a string and show only printable UTF-8 chars at the end + fix UTF-8 encoding.

EXAMPLE: UTF8::cleanup("\xEF\xBB\xBF„Abcdef\xc2\xa0\x20…” — 😃 - Düsseldorf", true, true); // '„Abcdef  …” — 😃 - Düsseldorf'

  • param string $str
  • psalm-pure
  • return string
public static codepoints( $arg, bool $use_u_style = false) : array
 

Accepts a string or an array of chars and returns an array of Unicode code points.

INFO: opposite to UTF8::string()

EXAMPLE: UTF8::codepoints('κöñ'); // array(954, 246, 241) // ... OR ... UTF8::codepoints('κöñ', true); // array('U+03ba', 'U+00f6', 'U+00f1')

  • param string|string[] $arg
  • param bool $use_u_style
  • psalm-pure
  • return int[]|string[] <p> The array of code points:<br> int[] for $u_style === false string[] for $u_style === true
  • template T as string|string[]
  • phpstan-param T $arg
  • phpstan-return (T is non-empty-string ? ($use_u_style is true ? non-empty-list : non-empty-list) : ($use_u_style is true ? list : list))
public static collapse_whitespace(string $str) : string
 

Trims the string and replaces consecutive whitespace characters with a single space. This includes tabs and newline characters, as well as multibyte whitespace such as the thin space and ideographic space.

  • param string $str
  • psalm-pure
  • return string <p>A string with trimmed $str and condensed whitespace.
public static count_chars(string $str, bool $clean_utf8 = false, bool $try_to_use_mb_functions = true) : array
 

Returns count of characters used in a string.

EXAMPLE: UTF8::count_chars('κaκbκc'); // array('κ' => 3, 'a' => 1, 'b' => 1, 'c' => 1)

  • param string $str
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param bool $try_to_use_mb_functions [optional] Set to false, if you don't want to use
  • psalm-pure
  • return int[]
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-array<string, int> : array<string, int>)
public static css_identifier(string $str = '', array $filter = [' ' => '-', '/' => '-', '[' => '', ']' => ''], bool $strip_tags = false, bool $strtolower = true) : string
 

Create a valid CSS identifier for e.g. "class"- or "id"-attributes.

EXAMPLE: UTF8::css_identifier('123foo/bar!!!'); // _23foo-bar

copy&past from https://github.com/drupal/core/blob/8.8.x/lib/Drupal/Component/Utility/Html.php#L95

  • param string $str
  • param string[] $filter
  • param bool $strip_tags
  • param bool $strtolower
  • psalm-pure
  • return string
  • phpstan-param array<string,string> $filter
public static css_stripe_media_queries(string $str) : string
 

Remove css media-queries.

  • param string $str
  • psalm-pure
  • return string
public static ctype_loaded() : bool
 

Checks whether ctype is available on the server.

  • psalm-pure
  • return bool
  • internal
public static decimal_to_chr( $int) : string
 

Converts an int value into a UTF-8 character.

INFO: opposite to UTF8::string()

EXAMPLE: UTF8::decimal_to_chr(931); // 'Σ'

  • param int|string $int
  • phpstan-param int|numeric-string $int
  • psalm-pure
  • return string
public static decode_mimeheader( $str, string $encoding = 'UTF-8')
 

Decodes a MIME header field

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return false|string
public static emoji_decode(string $str, bool $use_reversible_string_mappings = false) : string
 

Decodes a string which was encoded by "UTF8::emoji_encode()".

INFO: opposite to UTF8::emoji_encode()

EXAMPLE: UTF8::emoji_decode('foo CHARACTER_OGRE', false); // 'foo 👹' // UTF8::emoji_decode('foo -PORTABLE_UTF8-308095726-627590803-8FTU_ELBATROP-', true); // 'foo 👹'

  • param string $str
  • param bool $use_reversible_string_mappings [optional] When TRUE, we se a reversible string mapping between "emoji_encode" and "emoji_decode".
  • psalm-pure
  • return string
public static emoji_encode(string $str, bool $use_reversible_string_mappings = false) : string
 

Encode a string with emoji chars into a non-emoji string.

INFO: opposite to UTF8::emoji_decode()

EXAMPLE: UTF8::emoji_encode('foo 👹', false)); // 'foo CHARACTER_OGRE' // UTF8::emoji_encode('foo 👹', true)); // 'foo -PORTABLE_UTF8-308095726-627590803-8FTU_ELBATROP-'

  • param string $str
  • param bool $use_reversible_string_mappings [optional] when TRUE, we use a reversible string mapping between "emoji_encode" and "emoji_decode"
  • psalm-pure
  • return string
public static emoji_from_country_code(string $country_code_iso_3166_1) : string
 

Convert any two-letter country code (ISO 3166-1) to the corresponding Emoji.

  • see https://en.wikipedia.org/wiki/ISO_3166-1
  • param string $country_code_iso_3166_1
  • return string
public static encode(string $to_encoding, string $str, bool $auto_detect_the_from_encoding = true, string $from_encoding = '') : string
 

Encode a string with a new charset-encoding.

INFO: This function will also try to fix broken / double encoding, so you can call this function also on a UTF-8 string and you don't mess up the string.

EXAMPLE: UTF8::encode('ISO-8859-1', '-ABC-中文空白-'); // '-ABC-????-' // UTF8::encode('UTF-8', '-ABC-中文空白-'); // '-ABC-中文空白-' // UTF8::encode('HTML', '-ABC-中文空白-'); // '-ABC-中文空白-' // UTF8::encode('BASE64', '-ABC-中文空白-'); // 'LUFCQy3kuK3mlofnqbrnmb0t'

  • param string $to_encoding
  • param string $str
  • param bool $auto_detect_the_from_encoding [optional] Force the new encoding (we try to fix broken / double encoding for UTF-8) otherwise we auto-detect the current string-encoding
  • param string $from_encoding [optional] e.g. 'UTF-16', 'UTF-8', 'ISO-8859-1', etc. A empty string will trigger the autodetect anyway.
  • psalm-pure
  • return string
  • psalm-suppress InvalidReturnStatement
public static encode_mimeheader(string $str, string $from_charset = 'UTF-8', string $to_charset = 'UTF-8', string $transfer_encoding = 'Q', string $linefeed = ' ', int $indent = 76)
 
  • param string $str
  • param string $from_charset [optional] Set the input charset.
  • param string $to_charset [optional] Set the output charset.
  • param string $transfer_encoding [optional] Set the transfer encoding.
  • param string $linefeed [optional] Set the used linefeed.
  • param int<1, max> $indent [optional] Set the max length indent.
  • psalm-pure
  • return false|string
public static extract_text(string $str, string $search = '', ?int $length = NULL, string $replacer_for_skipped_text = '…', string $encoding = 'UTF-8') : string
 

Create an extract from a sentence, so if the search-string was found, it tries to center in the output.

  • param string $str
  • param string $search
  • param int|null $length [optional] Default: null === text->length / 2
  • param string $replacer_for_skipped_text [optional] Default: …
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static file_get_contents(string $filename, bool $use_include_path = false, $context = NULL, ?int $offset = NULL, ?int $max_length = NULL, int $timeout = 10, bool $convert_to_utf8 = true, string $from_encoding = '')
 

Reads entire file into a string.

EXAMPLE: UTF8::file_get_contents('utf16le.txt'); // ...

WARNING: Do not use UTF-8 Option ($convert_to_utf8) for binary files (e.g.: images) !!!

  • see http://php.net/manual/en/function.file-get-contents.php
  • param string $filename
  • param bool $use_include_path [optional] Prior to PHP 5, this parameter is called use_include_path and is a bool. As of PHP 5 the FILE_USE_INCLUDE_PATH can be used to trigger include path search.
  • param resource|null $context [optional] A valid context resource created with stream_context_create. If you don't need to use a custom context, you can skip this parameter by &null;.
  • param int|null $offset [optional] The offset where the reading starts.
  • param int<0, max>|null $max_length [optional] Maximum length of data read. The default is to read until end of file is reached.
  • param int $timeout
  • param bool $convert_to_utf8 WARNING!!! Maybe you can't use this option for some files, because they used non default utf-8 chars. Binary files like images or pdf will not be converted.
  • param string $from_encoding [optional] e.g. 'UTF-16', 'UTF-8', 'ISO-8859-1', etc. A empty string will trigger the autodetect anyway.
  • psalm-pure
  • return false|string
public static file_has_bom(string $file_path) : bool
 

Checks if a file starts with BOM (Byte Order Mark) character.

EXAMPLE: UTF8::file_has_bom('utf8_with_bom.txt'); // true

  • param string $file_path
  • throws \RuntimeException if file_get_contents() returned false
  • return bool
  • psalm-pure
public static filter( $var, int $normalization_form = 16Normalizer::NFC, string $leading_combining = '◌')
 

Normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.

EXAMPLE: UTF8::filter(array("\xE9", 'à', 'a')); // array('é', 'à', 'a')

  • param array|object|string $var
  • param int $normalization_form
  • param string $leading_combining
  • psalm-pure
  • return mixed
  • template TFilter
  • phpstan-param TFilter $var
  • phpstan-return TFilter
public static filter_input(int $type, string $variable_name, int $filter = 516FILTER_DEFAULT, $options = NULL)
 

"filter_input()"-wrapper with normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.

Gets a specific external variable by name and optionally filters it.

EXAMPLE: // _GET['foo'] = 'bar'; UTF8::filter_input(INPUT_GET, 'foo', FILTER_UNSAFE_RAW)); // 'bar'

  • see http://php.net/manual/en/function.filter-input.php
  • param int $type
  • param string $variable_name
  • param int $filter [optional] The ID of the filter to apply. The manual page lists the available filters.
  • param int|int[]|null $options [optional] Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array.
  • psalm-pure
  • return mixed
public static filter_input_array(int $type, $definition = NULL, bool $add_empty = true)
 

"filter_input_array()"-wrapper with normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.

Gets external variables and optionally filters them.

EXAMPLE: // _GET['foo'] = 'bar'; UTF8::filter_input_array(INPUT_GET, array('foo' => 'FILTER_UNSAFE_RAW')); // array('bar')

  • see http://php.net/manual/en/function.filter-input-array.php
  • param int $type
  • param array<string,mixed>|null $definition [optional] An array defining the arguments. A valid key is a string containing a variable name and a valid value is either a filter type, or an array optionally specifying the filter, flags and options. If the value is an array, valid keys are filter which specifies the filter type, flags which specifies any flags that apply to the filter, and options which specifies any options that apply to the filter. See the example below for a better understanding.
  • param bool $add_empty [optional] Add missing keys as NULL to the return value.
  • psalm-pure
  • return array<string,mixed>|false|null
public static filter_var( $variable, int $filter = 516FILTER_DEFAULT, $options = 0)
 

"filter_var()"-wrapper with normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.

Filters a variable with a specified filter.

EXAMPLE: UTF8::filter_var('-ABC-中文空白-', FILTER_VALIDATE_URL); // false

  • see http://php.net/manual/en/function.filter-var.php
  • param float|int|string|null $variable
  • param int $filter [optional] The ID of the filter to apply. The manual page lists the available filters.
  • param int|int[] $options [optional] <p> Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array. For the "callback" filter, callable type should be passed. The callback must accept one argument, the value to be filtered, and return the value after filtering/sanitizing it. </p> <p> <code> // for filters that accept options, use this format $options = array( 'options' => array( 'default' => 3, // value to return if the filter fails // other options here 'min_range' => 0 ), 'flags' => FILTER_FLAG_ALLOW_OCTAL, ); $var = filter_var('0755', FILTER_VALIDATE_INT, $options); // for filter that only accept flags, you can pass them directly $var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); // for filter that only accept flags, you can also pass as an array $var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)); // callback validate filter function foo($value) { // Expected format: Surname, GivenNames if (strpos($value, ", ") === false) return false; list($surname, $givennames) = explode(", ", $value, 2); $empty = (empty($surname) || empty($givennames)); $notstrings = (!is_string($surname) || !is_string($givennames)); if ($empty || $notstrings) { return false; } else { return $value; } } $var = filter_var('Doe, Jane Sue', FILTER_CALLBACK, array('options' => 'foo'));
  • psalm-pure
  • return mixed
public static filter_var_array(array $data, $definition = 0, bool $add_empty = true)
 

"filter_var_array()"-wrapper with normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.

Gets multiple variables and optionally filters them.

EXAMPLE: $filters = [ 'name' => ['filter' => FILTER_CALLBACK, 'options' => [UTF8::class, 'ucwords']], 'age' => ['filter' => FILTER_VALIDATE_INT, 'options' => ['min_range' => 1, 'max_range' => 120]], 'email' => FILTER_VALIDATE_EMAIL, ];

$data = [ 'name' => 'κόσμε', 'age' => '18', 'email' => 'foo@bar.de' ];

UTF8::filter_var_array($data, $filters, true); // ['name' => 'Κόσμε', 'age' => 18, 'email' => 'foo@bar.de']

  • see http://php.net/manual/en/function.filter-var-array.php
  • param array<string,mixed> $data
  • param array<string,mixed>|int $definition [optional] An array defining the arguments. A valid key is a string containing a variable name and a valid value is either a filter type, or an array optionally specifying the filter, flags and options. If the value is an array, valid keys are filter which specifies the filter type, flags which specifies any flags that apply to the filter, and options which specifies any options that apply to the filter. See the example below for a better understanding.
  • param bool $add_empty [optional] Add missing keys as NULL to the return value.
  • psalm-pure
  • return array<string,mixed>|false|null
public static finfo_loaded() : bool
 

Checks whether finfo is available on the server.

  • psalm-pure
  • return bool
  • internal
public static first_char(string $str, int $n = 1, string $encoding = 'UTF-8') : string
 

Returns the first $n characters of the string.

  • param string $str
  • param int<1, max> $n
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static fits_inside(string $str, int $box_size) : bool
 

Check if the number of Unicode characters isn't greater than the specified integer.

EXAMPLE: UTF8::fits_inside('κόσμε', 6); // false

  • param string $str the original string to be checked
  • param int $box_size the size in number of chars to be checked against string
  • psalm-pure
  • return bool <p><strong>TRUE</strong> if string is less than or equal to $box_size, FALSE otherwise.
public static fix_simple_utf8(string $str) : string
 

Try to fix simple broken UTF-8 strings.

INFO: Take a look at "UTF8::fix_utf8()" if you need a more advanced fix for broken UTF-8 strings.

EXAMPLE: UTF8::fix_simple_utf8('Düsseldorf'); // 'Düsseldorf'

If you received an UTF-8 string that was converted from Windows-1252 as it was ISO-8859-1 (ignoring Windows-1252 chars from 80 to 9F) use this function to fix it. See: http://en.wikipedia.org/wiki/Windows-1252

  • param string $str
  • psalm-pure
  • return string
public static fix_utf8( $str)
 

Fix a double (or multiple) encoded UTF8 string.

EXAMPLE: UTF8::fix_utf8('Fédération'); // 'Fédération'

  • param string|string[] $str you can use a string or an array of strings
  • psalm-pure
  • return string|string[]
  • template TFixUtf8 as string|string[]
  • phpstan-param TFixUtf8 $str
  • phpstan-return TFixUtf8
public static get_file_type(string $str, array $fallback = ['ext' => NULL, 'mime' => 'application/octet-stream', 'type' => NULL]) : array
 

Warning: this method only works for some file-types (png, jpg) if you need more supported types, please use e.g. "finfo"

  • param string $str
  • param array{ext: null|string, mime: null|string, type: null|string} $fallback
  • return array{ext: null|string, mime: null|string, type: null|string}
  • psalm-pure
public static get_random_string(int $length, string $possible_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', string $encoding = 'UTF-8') : string
 
  • param int<1, max> $length
  • param string $possible_chars [optional] Characters string for the random selection.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • return string
  • template T as string
  • phpstan-param T $possible_chars
  • phpstan-return (T is non-empty-string ? non-empty-string : '')
public static get_unique_string( $extra_entropy = '', bool $use_md5 = true) : string
 
  • param int|string $extra_entropy [optional] Extra entropy via a string or int value.
  • param bool $use_md5 [optional] Return the unique identifier as md5-hash? Default: true
  • return non-empty-string
public static getCharDirection(string $char) : string
 

Get character of a specific character.

EXAMPLE: UTF8::getCharDirection('ا'); // 'RTL'

  • param string $char
  • psalm-pure
  • return string
public static getSupportInfo(?string $key = NULL)
 

Check for php-support.

  • param string|null $key
  • psalm-pure
  • return mixed Return the full support-"array", if $key === null return bool-value, if $key is used and available otherwise return null
public static getUrlParamFromArray(string $param, array $data)
 

Get data from an array via array like string.

EXAMPLE: $array['foo'][123] = 'lall'; UTF8::getUrlParamFromArray('foo[123]', $array); // 'lall'

  • param array<array-key,mixed> $data
  • return mixed
public static has_lowercase(string $str) : bool
 

Returns true if the string contains a lower case char, false otherwise.

  • param string $str
  • psalm-pure
  • return bool
public static has_uppercase(string $str) : bool
 

Returns true if the string contains an upper case char, false otherwise.

  • param string $str
  • psalm-pure
  • return bool
public static has_whitespace(string $str) : bool
 

Returns true if the string contains whitespace, false otherwise.

  • param string $str
  • psalm-pure
  • return bool
public static hex_to_chr(string $hexdec)
 

Converts a hexadecimal value into a UTF-8 character.

INFO: opposite to UTF8::chr_to_hex()

EXAMPLE: UTF8::hex_to_chr('U+00a7'); // '§'

  • param string $hexdec
  • psalm-pure
  • return string
public static hex_to_int( $hexdec)
 

Converts hexadecimal U+xxxx code point representation to integer.

INFO: opposite to UTF8::int_to_hex()

EXAMPLE: UTF8::hex_to_int('U+00f1'); // 241

  • param string $hexdec
  • psalm-pure
  • return false|int
public static html_encode(string $str, bool $keep_ascii_chars = false, string $encoding = 'UTF-8') : string
 

Converts a UTF-8 string to a series of HTML numbered entities.

INFO: opposite to UTF8::html_decode()

EXAMPLE: UTF8::html_encode('中文空白'); // '中文空白'

  • param string $str
  • param bool $keep_ascii_chars [optional] Keep ASCII chars.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static html_entity_decode(string $str, ?int $flags = NULL, string $encoding = 'UTF-8') : string
 

UTF-8 version of html_entity_decode()

The reason we are not using html_entity_decode() by itself is because while it is not technically correct to leave out the semicolon at the end of an entity most browsers will still interpret the entity correctly. html_entity_decode() does not convert entities without semicolons, so we are left with our own little solution here. Bummer.

Convert all HTML entities to their applicable characters.

INFO: opposite to UTF8::html_encode()

EXAMPLE: UTF8::html_entity_decode('中文空白'); // '中文空白'

  • see http://php.net/manual/en/function.html-entity-decode.php
  • param string $str
  • param int|null $flags [optional] A bitmask of one or more of the following flags, which specify how to handle quotes and which document type to use. The default is ENT_COMPAT | ENT_HTML401.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static html_escape(string $str, string $encoding = 'UTF-8') : string
 

Create a escape html version of the string via "UTF8::htmlspecialchars()".

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static html_stripe_empty_tags(string $str) : string
 

Remove empty html-tag.

e.g.:

  • param string $str
  • psalm-pure
  • return string
public static htmlentities(string $str, int $flags = 2ENT_COMPAT, string $encoding = 'UTF-8', bool $double_encode = true) : string
 

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
  • 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.
  • param string $encoding [optional] 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
public static htmlspecialchars(string $str, int $flags = 2ENT_COMPAT, string $encoding = 'UTF-8', bool $double_encode = true) : string
 

Convert only special characters to HTML entities: UTF-8 version of htmlspecialchars()

INFO: Take a look at "UTF8::htmlentities()"

EXAMPLE: UTF8::htmlspecialchars('<白-öäü>'); // '<白-öäü>'

  • see http://php.net/manual/en/function.htmlspecialchars.php
  • param string $str
  • 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.
  • param string $encoding [optional] Defines encoding used in conversion.
  • 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
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static iconv_loaded() : bool
 

Checks whether iconv is available on the server.

  • psalm-pure
  • return bool
  • internal
public static int_to_hex(int $int, string $prefix = 'U+') : string
 

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
  • param string $prefix [optional]
  • psalm-pure
  • return string the code point, or empty string on failure
public static intl_loaded() : bool
 

Checks whether intl is available on the server.

  • psalm-pure
  • return bool
  • internal
public static intlChar_loaded() : bool
 

Checks whether intl-char is available on the server.

  • psalm-pure
  • return bool
  • internal
public static is_alpha(string $str) : bool
 

Returns true if the string contains only alphabetic chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only alphabetic chars.
public static is_alphanumeric(string $str) : bool
 

Returns true if the string contains only alphabetic and numeric chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only alphanumeric chars.
public static is_ascii(string $str) : bool
 

Checks if a string is 7 bit ASCII.

EXAMPLE: UTF8::is_ascii('白'); // false

  • param string $str
  • psalm-pure
  • return bool
public static is_base64( $str, bool $empty_string_is_valid = false) : bool
 

Returns true if the string is base64 encoded, false otherwise.

EXAMPLE: UTF8::is_base64('4KSu4KWL4KSo4KS/4KSa'); // true

  • param string|null $str
  • param bool $empty_string_is_valid [optional] Is an empty string valid base64 or not?
  • psalm-pure
  • return bool <p>Whether or not $str is base64 encoded.
public static is_binary( $input, bool $strict = false) : bool
 

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 is_binary_file( $file) : bool
 

Check if the file is binary.

EXAMPLE: UTF8::is_binary('./utf32.txt'); // true

  • param string $file
  • return bool
public static is_blank(string $str) : bool
 

Returns true if the string contains only whitespace chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only whitespace characters.
public static is_bom( $str) : bool
 

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
  • psalm-pure
  • return bool <p><strong>true</strong> if the $utf8_chr is Byte Order Mark, false otherwise.
public static is_empty( $str) : bool
 

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 array<array-key,mixed>|float|int|string $str
  • psalm-pure
  • return bool <p>Whether or not $str is empty().
public static is_hexadecimal(string $str) : bool
 

Returns true if the string contains only hexadecimal chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only hexadecimal chars.
public static is_html(string $str) : bool
 

Check if the string contains any HTML tags.

EXAMPLE: UTF8::is_html('lall'); // true

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains html elements.
public static is_json(string $str, bool $only_array_or_object_results_are_valid = true) : bool
 

Try to check if "$str" is a JSON-string.

EXAMPLE: UTF8::is_json('{"array":[1,"¥","ä"]}'); // true

  • param string $str
  • param bool $only_array_or_object_results_are_valid [optional] Only array and objects are valid json results.
  • return bool <p>Whether or not the $str is in JSON format.
public static is_lowercase(string $str) : bool
 
  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only lowercase chars.
public static is_printable(string $str, bool $ignore_control_characters = false) : bool
 

Returns true if the string contains only printable (non-invisible) chars, false otherwise.

  • param string $str
  • param bool $ignore_control_characters [optional] Ignore control characters like [LRM] or [LSEP].
  • psalm-pure
  • return bool <p>Whether or not $str contains only printable (non-invisible) chars.
public static is_punctuation(string $str) : bool
 

Returns true if the string contains only punctuation chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only punctuation chars.
public static is_serialized(string $str) : bool
 

Returns true if the string is serialized, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str is serialized.
public static is_uppercase(string $str) : bool
 

Returns true if the string contains only lower case chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only lower case characters.
public static is_url(string $url, bool $disallow_localhost = false) : bool
 

Check if $url is an correct url.

  • param string $url
  • param bool $disallow_localhost
  • psalm-pure
  • return bool
public static is_utf16( $str, bool $check_if_string_is_binary = true)
 

Check if the string is UTF-16.

EXAMPLE: UTF8::is_utf16(file_get_contents('utf-16-le.txt')); // 1 // UTF8::is_utf16(file_get_contents('utf-16-be.txt')); // 2 // UTF8::is_utf16(file_get_contents('utf-8.txt')); // false

  • param string $str
  • param bool $check_if_string_is_binary
  • psalm-pure
  • return false|int false if is't not UTF-16, 1 for UTF-16LE, 2 for UTF-16BE
public static is_utf32( $str, bool $check_if_string_is_binary = true)
 

Check if the string is UTF-32.

EXAMPLE: UTF8::is_utf32(file_get_contents('utf-32-le.txt')); // 1 // UTF8::is_utf32(file_get_contents('utf-32-be.txt')); // 2 // UTF8::is_utf32(file_get_contents('utf-8.txt')); // false

  • param string $str
  • param bool $check_if_string_is_binary
  • psalm-pure
  • return false|int false if is't not UTF-32, 1 for UTF-32LE, 2 for UTF-32BE
public static is_utf8( $str, bool $strict = false) : bool
 

Checks whether the passed input contains only byte sequences that appear valid UTF-8.

EXAMPLE: UTF8::is_utf8(['Iñtërnâtiônàlizætiøn', 'foo']); // true // UTF8::is_utf8(["Iñtërnâtiônàlizætiøn\xA0\xA1", 'bar']); // false

  • param int|string|string[]|null $str
  • param bool $strict
  • psalm-pure
  • return bool
public static json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
 

(PHP 5 >= 5.2.0, PECL json >= 1.2.0) Decodes a JSON string

EXAMPLE: UTF8::json_decode('[1,"¥","ä"]'); // array(1, '¥', 'ä')

  • see http://php.net/manual/en/function.json-decode.php
  • param string $json
  • param bool $assoc [optional] When TRUE, returned objects will be converted into associative arrays.
  • param int $depth [optional] User specified recursion depth.
  • param int $options [optional] Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)
  • psalm-pure
  • return mixed
public static json_encode( $value, int $options = 0, int $depth = 512)
 

(PHP 5 >= 5.2.0, PECL json >= 1.2.0) Returns the JSON representation of a value.

EXAMPLE: UTF8::json_encode(array(1, '¥', 'ä')); // '[1,"¥","ä"]'

  • see http://php.net/manual/en/function.json-encode.php
  • param mixed $value
  • param int $options [optional] Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE. The behaviour of these constants is described on the JSON constants page.
  • param int $depth [optional] Set the maximum depth. Must be greater than zero.
  • psalm-pure
  • return false|string
public static json_loaded() : bool
 

Checks whether JSON is available on the server.

  • psalm-pure
  • return bool
  • internal
public static lcfirst(string $str, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Makes string's first char lowercase.

EXAMPLE: UTF8::lcfirst('ÑTËRNÂTIÔNÀLIZÆTIØN'); // ñTËRNÂTIÔNÀLIZÆTIØN

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
public static lcwords(string $str, array $exceptions = [], string $char_list = '', string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Lowercase for all words in the string.

  • param string $str
  • param string[] $exceptions [optional] Exclusion for some words.
  • param string $char_list [optional] Additional chars that contains to words and do not start a new word.
  • param string $encoding [optional] Set the charset.
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
public static levenshtein(string $str1, string $str2, int $insertionCost = 1, int $replacementCost = 1, int $deletionCost = 1) : int
 

Calculate Levenshtein distance between two strings.

For better performance, in a real application with a single input string matched against many strings from a database, you will probably want to pre- encode the input only once and use \levenshtein().

Source: https://github.com/KEINOS/mb_levenshtein

  • see https://www.php.net/manual/en/function.levenshtein
  • param string $str1
  • param string $str2
  • param int $insertionCost [optional] Defines the cost of insertion.
  • param int $replacementCost [optional] Defines the cost of replacement.
  • param int $deletionCost [optional] Defines the cost of deletion.
  • return int
public static ltrim(string $str = '', ?string $chars = NULL) : string
 

Strip whitespace or other characters from the beginning of a UTF-8 string.

EXAMPLE: UTF8::ltrim(' 中文空白  '); // '中文空白  '

  • param string $str
  • param string|null $chars
  • psalm-pure
  • return string the string with unwanted characters stripped from the left
public static max( $arg)
 

Returns the UTF-8 character with the maximum code point in the given data.

EXAMPLE: UTF8::max('abc-äöü-中文空白'); // 'ø'

  • param string|string[] $arg
  • psalm-pure
  • return string|null the character with the highest code point than others, returns null on failure or empty input
public static max_chr_width(string $str) : int
 

Calculates and returns the maximum number of bytes taken by any UTF-8 encoded character in the given string.

EXAMPLE: UTF8::max_chr_width('Intërnâtiônàlizætiøn'); // 2

  • param string $str
  • psalm-pure
  • return int
  • phpstan-return 0|1|2|3|4
public static mbstring_loaded() : bool
 

Checks whether mbstring is available on the server.

  • psalm-pure
  • return bool
  • internal
public static min( $arg)
 

Returns the UTF-8 character with the minimum code point in the given data.

EXAMPLE: UTF8::min('abc-äöü-中文空白'); // '-'

  • param string|string[] $arg A UTF-8 encoded string or an array of such strings.
  • psalm-pure
  • return string|null
public static normalize_encoding( $encoding, $fallback = '')
 

Normalize the encoding-"name" input.

EXAMPLE: UTF8::normalize_encoding('UTF8'); // 'UTF-8'

  • param mixed $encoding
  • param mixed $fallback
  • psalm-pure
  • return mixed|string
  • template TNormalizeEncodingFallback
  • phpstan-param string|TNormalizeEncodingFallback $fallback
  • phpstan-return string|TNormalizeEncodingFallback
public static normalize_line_ending(string $str, $replacer = ' ') : string
 

Standardize line ending to unix-like.

  • param string $str
  • param string|string[] $replacer
  • psalm-pure
  • return string
public static normalize_msword(string $str) : string
 

Normalize some MS Word special characters.

EXAMPLE: UTF8::normalize_msword('„Abcdef…”'); // '"Abcdef..."'

  • param string $str
  • psalm-pure
  • return string
public static normalize_whitespace(string $str, bool $keep_non_breaking_space = false, bool $keep_bidi_unicode_controls = false, bool $normalize_control_characters = false) : string
 

Normalize the whitespace.

EXAMPLE: UTF8::normalize_whitespace("abc-\xc2\xa0-öäü-\xe2\x80\xaf-\xE2\x80\xAC", true); // "abc-\xc2\xa0-öäü- -"

  • param string $str
  • param bool $keep_non_breaking_space [optional] Set to true, to keep non-breaking-spaces.
  • param bool $keep_bidi_unicode_controls [optional] Set to true, to keep non-printable (for the web) bidirectional text chars.
  • param bool $normalize_control_characters [optional] Set to true, to convert e.g. LINE-, PARAGRAPH-SEPARATOR with "\n" and LINE TABULATION with "\t".
  • psalm-pure
  • return string
public static ord( $chr, string $encoding = 'UTF-8') : int
 

Calculates Unicode code point of the given UTF-8 encoded character.

INFO: opposite to UTF8::chr()

EXAMPLE: UTF8::ord('☃'); // 0x2603

  • param string $chr
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return int
public static parse_str(string $str, $result, bool $clean_utf8 = false) : bool
 

Parses the string into an array (into the the second parameter).

WARNING: Unlike "parse_str()", this method does not (re-)place variables in the current scope, if the second parameter is not set!

EXAMPLE: UTF8::parse_str('Iñtërnâtiônéàlizætiøn=測試&arr[]=foo+測試&arr[]=ການທົດສອບ', $array); echo $array['Iñtërnâtiônéàlizætiøn']; // '測試'

  • see http://php.net/manual/en/function.parse-str.php
  • param string $str
  • param array<string,mixed> $result
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return bool <p>Will return <strong>false</strong> if php can't parse the string and we haven't any $result.
public static pcre_utf8_support() : bool
 

Checks if \u modifier is available that enables Unicode support in PCRE.

  • psalm-pure
  • return bool
public static range( $var1, $var2, bool $use_ctype = true, string $encoding = 'UTF-8', $step = 1) : array
 

Create an array containing a range of UTF-8 characters.

EXAMPLE: UTF8::range('κ', 'ζ'); // array('κ', 'ι', 'θ', 'η', 'ζ',)

  • param int|string $var1
  • param int|string $var2
  • param bool $use_ctype
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param float|int $step [optional] If a step value is given, it will be used as the increment between elements in the sequence. step should be given as a positive number. If not specified, step will default to 1.
  • psalm-pure
  • return list<string>
public static rawurldecode(string $str, bool $multi_decode = true) : string
 

Multi decode HTML entity + fix urlencoded-win1252-chars.

EXAMPLE: UTF8::rawurldecode('tes%20öäü%20ítest+test'); // 'tes öäü ítest+test'

e.g: 'test+test' => 'test+test' 'Düsseldorf' => 'Düsseldorf' 'D%FCsseldorf' => 'Düsseldorf' 'Düsseldorf' => 'Düsseldorf' 'D%26%23xFC%3Bsseldorf' => 'Düsseldorf' 'Düsseldorf' => 'Düsseldorf' 'D%C3%BCsseldorf' => 'Düsseldorf' 'D%C3%83%C2%BCsseldorf' => 'Düsseldorf' 'D%25C3%2583%25C2%25BCsseldorf' => 'Düsseldorf'

  • param string $str
  • param bool $multi_decode
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static regex_replace(string $str, string $pattern, string $replacement, string $options = '', string $delimiter = '/') : string
 

Replaces all occurrences of $pattern in $str by $replacement.

  • param string $str
  • param string $pattern
  • param string $replacement
  • param string $options [optional] Matching conditions to be used.
  • param string $delimiter [optional] Delimiter the the regex. Default: '/'
  • psalm-pure
  • return string
public static remove_bom(string $str) : string
 

Remove the BOM from UTF-8 / UTF-16 / UTF-32 strings.

EXAMPLE: UTF8::remove_bom("\xEF\xBB\xBFΜπορώ να"); // 'Μπορώ να'

  • param string $str
  • psalm-pure
  • return string
public static remove_duplicates(string $str, $what = ' ') : string
 

Removes duplicate occurrences of a string in another string.

EXAMPLE: UTF8::remove_duplicates('öäü-κόσμεκόσμε-äöü', 'κόσμε'); // 'öäü-κόσμε-äöü'

  • param string $str
  • param string|string[] $what
  • psalm-pure
  • return string
public static remove_html(string $str, string $allowable_tags = '') : string
 

Remove html via "strip_tags()" from the string.

  • param string $str
  • param string $allowable_tags [optional] You can use the optional second parameter to specify tags which should not be stripped. Default: null
  • psalm-pure
  • return string
public static remove_html_breaks(string $str, string $replacement = '') : string
 

Remove all breaks [ | \r\n | \r | \n | ...] from the string.

  • param string $str
  • param string $replacement [optional] Default is a empty string.
  • psalm-pure
  • return string
public static remove_ileft(string $str, string $substring, string $encoding = 'UTF-8') : string
 

Returns a new string with the prefix $substring removed, if present and case-insensitive.

  • param string $str
  • param string $substring
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string <p>A string without the prefix $substring.
public static remove_invisible_characters(string $str, bool $url_encoded = false, string $replacement = '', bool $keep_basic_control_characters = true) : string
 

Remove invisible characters from a string.

e.g.: This prevents sandwiching null characters between ascii characters, like Java\0script.

EXAMPLE: UTF8::remove_invisible_characters("κόσ\0με"); // 'κόσμε'

copy&past from https://github.com/bcit-ci/CodeIgniter/blob/develop/system/core/Common.php

  • param string $str
  • param bool $url_encoded [optional] Try to remove url encoded control character. WARNING: maybe contains false-positives e.g. aa%0Baa -> aaaa. Default: false
  • param string $replacement [optional] The replacement character.
  • param bool $keep_basic_control_characters [optional] Keep control characters like [LRM] or [LSEP].
  • psalm-pure
  • return string
public static remove_iright(string $str, string $substring, string $encoding = 'UTF-8') : string
 

Returns a new string with the suffix $substring removed, if present and case-insensitive.

  • param string $str
  • param string $substring
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string <p>A string having a $str without the suffix $substring.
public static remove_left(string $str, string $substring, string $encoding = 'UTF-8') : string
 

Returns a new string with the prefix $substring removed, if present.

  • param string $str
  • param string $substring
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string <p>A string without the prefix $substring.
public static remove_right(string $str, string $substring, string $encoding = 'UTF-8') : string
 

Returns a new string with the suffix $substring removed, if present.

  • param string $str
  • param string $substring
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string <p>A string having a $str without the suffix $substring.
public static replace(string $str, string $search, string $replacement, bool $case_sensitive = true) : string
 

Replaces all occurrences of $search in $str by $replacement.

  • param string $str
  • param string $search
  • param string $replacement
  • param bool $case_sensitive [optional] Whether or not to enforce case-sensitivity. Default: true
  • psalm-pure
  • return string
public static replace_all(string $str, array $search, $replacement, bool $case_sensitive = true) : string
 

Replaces all occurrences of $search in $str by $replacement.

  • param string $str
  • param string[] $search
  • param string|string[] $replacement
  • param bool $case_sensitive [optional] Whether or not to enforce case-sensitivity. Default: true
  • psalm-pure
  • return string
public static replace_diamond_question_mark(string $str, string $replacement_char = '', bool $process_invalid_utf8_chars = true) : string
 

Replace the diamond question mark (�) and invalid-UTF8 chars with the replacement.

EXAMPLE: UTF8::replace_diamond_question_mark('中文空白�', ''); // '中文空白'

  • param string $str
  • param string $replacement_char
  • param bool $process_invalid_utf8_chars
  • psalm-pure
  • return string
public static rtrim(string $str = '', ?string $chars = NULL) : string
 

Strip whitespace or other characters from the end of a UTF-8 string.

EXAMPLE: UTF8::rtrim('-ABC-中文空白- '); // '-ABC-中文空白-'

  • param string $str
  • param string|null $chars
  • psalm-pure
  • return string
public static showSupport(bool $useEcho = true)
 

WARNING: Print native UTF-8 support (libs) by default, e.g. for debugging.

  • param bool $useEcho
  • psalm-pure
  • return string|void
  • phpstan-return ($useEcho is true ? void : string)
public static single_chr_html_encode(string $char, bool $keep_ascii_chars = false, string $encoding = 'UTF-8') : string
 

Converts a UTF-8 character to HTML Numbered Entity like "{".

EXAMPLE: UTF8::single_chr_html_encode('κ'); // 'κ'

  • param string $char
  • param bool $keep_ascii_chars
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $char
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static spaces_to_tabs(string $str, int $tab_length = 4) : string
 
  • param string $str
  • param int<1, max> $tab_length
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static str_camelize(string $str, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Returns a camelCase version of the string. Trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, as well as underscores.

  • param string $str
  • param string $encoding [optional] Default: 'UTF-8'
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
public static str_capitalize_name(string $str) : string
 

Returns the string with the first letter of each word capitalized, except for when the word is a name which shouldn't be capitalized.

  • param string $str
  • psalm-pure
  • return string <p>A string with $str capitalized.
public static str_contains(string $haystack, string $needle, bool $case_sensitive = true) : bool
 

Returns true if the string contains $needle, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $case_sensitive to false.

  • param string $haystack
  • param string $needle
  • param bool $case_sensitive [optional] Whether or not to enforce case-sensitivity. Default: true
  • psalm-pure
  • return bool <p>Whether or not $haystack contains $needle.
public static str_contains_all(string $haystack, array $needles, bool $case_sensitive = true) : bool
 

Returns true if the string contains all $needles, false otherwise. By default, the comparison is case-sensitive, but can be made insensitive by setting $case_sensitive to false.

  • param string $haystack
  • param scalar[] $needles
  • param bool $case_sensitive [optional] Whether or not to enforce case-sensitivity. Default: true
  • psalm-pure
  • return bool <p>Whether or not $haystack contains $needle.
public static str_contains_any(string $haystack, array $needles, bool $case_sensitive = true) : bool
 

Returns true if the string contains any $needles, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $case_sensitive to false.

  • param string $haystack
  • param scalar[] $needles
  • param bool $case_sensitive [optional] Whether or not to enforce case-sensitivity. Default: true
  • psalm-pure
  • return bool <p>Whether or not $str contains $needle.
public static str_dasherize(string $str, string $encoding = 'UTF-8') : string
 

Returns a lowercase and trimmed string separated by dashes. Dashes are inserted before uppercase characters (with the exception of the first character of the string), and in place of spaces as well as underscores.

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static str_delimit(string $str, string $delimiter, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Returns a lowercase and trimmed string separated by the given delimiter.

Delimiters are inserted before uppercase characters (with the exception of the first character of the string), and in place of spaces, dashes, and underscores. Alpha delimiters are not converted to lowercase.

EXAMPLE: UTF8::str_delimit('test case, '#'); // 'test#case' UTF8::str_delimit('test -case', ''); // 'testcase'

  • param string $str
  • param string $delimiter
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static str_detect_encoding( $str)
 

Optimized "mb_detect_encoding()"-function -> with support for UTF-16 and UTF-32.

EXAMPLE: UTF8::str_detect_encoding('中文空白'); // 'UTF-8' UTF8::str_detect_encoding('Abc'); // 'ASCII'

  • param string $str
  • psalm-pure
  • return false|string
public static str_ends_with(string $haystack, string $needle) : bool
 

Check if the string ends with the given substring.

EXAMPLE: UTF8::str_ends_with('BeginMiddleΚόσμε', 'Κόσμε'); // true UTF8::str_ends_with('BeginMiddleΚόσμε', 'κόσμε'); // false

  • param string $haystack
  • param string $needle
  • psalm-pure
  • return bool
public static str_ends_with_any(string $str, array $substrings) : bool
 

Returns true if the string ends with any of $substrings, false otherwise.

  • case-sensitive
  • param string $str
  • param string[] $substrings
  • psalm-pure
  • return bool <p>Whether or not $str ends with $substring.
public static str_ensure_left(string $str, string $substring) : string
 

Ensures that the string begins with $substring. If it doesn't, it's prepended.

  • param string $str
  • param string $substring
  • psalm-pure
  • template T as string
  • template TSub as string
  • phpstan-param T $str
  • phpstan-param TSub $substring
  • phpstan-return (TSub is non-empty-string ? non-empty-string : (T is non-empty-string ? non-empty-string : string))
public static str_ensure_right(string $str, string $substring) : string
 

Ensures that the string ends with $substring. If it doesn't, it's appended.

  • param string $str
  • param string $substring
  • psalm-pure
  • return string
  • template T as string
  • template TSub as string
  • phpstan-param T $str
  • phpstan-param TSub $substring
  • phpstan-return (TSub is non-empty-string ? non-empty-string : (T is non-empty-string ? non-empty-string : string))
public static str_humanize( $str) : string
 

Capitalizes the first word of the string, replaces underscores with spaces, and strips '_id'.

  • param string $str
  • psalm-pure
  • return string
public static str_iends_with(string $haystack, string $needle) : bool
 

Check if the string ends with the given substring, case-insensitive.

EXAMPLE: UTF8::str_iends_with('BeginMiddleΚόσμε', 'Κόσμε'); // true UTF8::str_iends_with('BeginMiddleΚόσμε', 'κόσμε'); // true

  • param string $haystack
  • param string $needle
  • psalm-pure
  • return bool
public static str_iends_with_any(string $str, array $substrings) : bool
 

Returns true if the string ends with any of $substrings, false otherwise.

  • case-insensitive
  • param string $str
  • param string[] $substrings
  • psalm-pure
  • return bool <p>Whether or not $str ends with $substring.
public static str_insert(string $str, string $substring, int $index, string $encoding = 'UTF-8') : string
 

Inserts $substring into the string at the $index provided.

  • param string $str
  • param string $substring
  • param int $index
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static str_ireplace( $search, $replacement, $subject, $count = NULL)
 

Case-insensitive and UTF-8 safe version of str_replace.

EXAMPLE: UTF8::str_ireplace('lIzÆ', 'lise', 'Iñtërnâtiônàlizætiøn'); // 'Iñtërnâtiônàlisetiøn'

  • see http://php.net/manual/en/function.str-ireplace.php
  • param string|string[] $search
  • param string|string[] $replacement
  • param string|string[] $subject
  • param int $count [optional] The number of matched and replaced needles will be returned in count which is passed by reference.
  • psalm-pure
  • return string|string[]
  • template TStrIReplaceSubject
  • phpstan-param TStrIReplaceSubject $subject
  • phpstan-return TStrIReplaceSubject
public static str_ireplace_beginning(string $str, string $search, string $replacement) : string
 

Replaces $search from the beginning of string with $replacement.

  • param string $str
  • param string $search
  • param string $replacement
  • psalm-pure
  • return string
public static str_ireplace_ending(string $str, string $search, string $replacement) : string
 

Replaces $search from the ending of string with $replacement.

  • param string $str
  • param string $search
  • param string $replacement
  • psalm-pure
  • return string
public static str_istarts_with(string $haystack, string $needle) : bool
 

Check if the string starts with the given substring, case-insensitive.

EXAMPLE: UTF8::str_istarts_with('ΚόσμεMiddleEnd', 'Κόσμε'); // true UTF8::str_istarts_with('ΚόσμεMiddleEnd', 'κόσμε'); // true

  • param string $haystack
  • param string $needle
  • psalm-pure
  • return bool
public static str_istarts_with_any(string $str, array $substrings) : bool
 

Returns true if the string begins with any of $substrings, false otherwise.

  • case-insensitive
  • param string $str
  • param scalar[] $substrings
  • psalm-pure
  • return bool <p>Whether or not $str starts with $substring.
public static str_isubstr_after_first_separator(string $str, string $separator, string $encoding = 'UTF-8') : string
 

Gets the substring after the first occurrence of a separator.

  • param string $str
  • param string $separator
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_isubstr_after_last_separator(string $str, string $separator, string $encoding = 'UTF-8') : string
 

Gets the substring after the last occurrence of a separator.

  • param string $str
  • param string $separator
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_isubstr_before_first_separator(string $str, string $separator, string $encoding = 'UTF-8') : string
 

Gets the substring before the first occurrence of a separator.

  • param string $str
  • param string $separator
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_isubstr_before_last_separator(string $str, string $separator, string $encoding = 'UTF-8') : string
 

Gets the substring before the last occurrence of a separator.

  • param string $str
  • param string $separator
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_isubstr_first(string $str, string $needle, bool $before_needle = false, string $encoding = 'UTF-8') : string
 

Gets the substring after (or before via "$before_needle") the first occurrence of the "$needle".

  • param string $str
  • param string $needle
  • param bool $before_needle [optional] Default: false
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_isubstr_last(string $str, string $needle, bool $before_needle = false, string $encoding = 'UTF-8') : string
 

Gets the substring after (or before via "$before_needle") the last occurrence of the "$needle".

  • param string $str
  • param string $needle
  • param bool $before_needle [optional] Default: false
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_last_char(string $str, int $n = 1, string $encoding = 'UTF-8') : string
 

Returns the last $n characters of the string.

  • param string $str
  • param int $n
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static str_limit(string $str, int $length = 100, string $str_add_on = '…', string $encoding = 'UTF-8') : string
 

Limit the number of characters in a string.

  • param string $str
  • param int<1, max> $length [optional] Default: 100
  • param string $str_add_on [optional] Default: …
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static str_limit_after_word(string $str, int $length = 100, string $str_add_on = '…', string $encoding = 'UTF-8') : string
 

Limit the number of characters in a string, but also after the next word.

EXAMPLE: UTF8::str_limit_after_word('fòô bàř fòô', 8, ''); // 'fòô bàř'

  • param string $str
  • param int<1, max> $length [optional] Default: 100
  • param string $str_add_on [optional] Default: …
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static str_limit_in_byte(string $str, int $length = 100, string $str_add_on = '...', string $encoding = 'UTF-8') : string
 

Limit the number of characters in a string in bytes.

  • param string $str
  • param int<1, max> $length [optional] Default: 100
  • param string $str_add_on [optional] Default: ...
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static str_longest_common_prefix(string $str1, string $str2, string $encoding = 'UTF-8') : string
 

Returns the longest common prefix between the $str1 and $str2.

  • param string $str1
  • param string $str2
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static str_longest_common_substring(string $str1, string $str2, string $encoding = 'UTF-8') : string
 

Returns the longest common substring between the $str1 and $str2.

In the case of ties, it returns that which occurs first.

  • param string $str1
  • param string $str2
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string <p>A string with its $str being the longest common substring.
public static str_longest_common_suffix(string $str1, string $str2, string $encoding = 'UTF-8') : string
 

Returns the longest common suffix between the $str1 and $str2.

  • param string $str1
  • param string $str2
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static str_matches_pattern(string $str, string $pattern) : bool
 

Returns true if $str matches the supplied pattern, false otherwise.

  • param string $str
  • param string $pattern
  • psalm-pure
  • return bool <p>Whether or not $str matches the pattern.
public static str_obfuscate(string $str, float $percent = 0.5, string $obfuscateChar = '*', array $keepChars = []) : string
 

Convert a string into a obfuscate string.

EXAMPLE:

UTF8::str_obfuscate('lars@moelleken.org', 0.5, '', ['@', '.']); // e.g. "l**@m**lleke*.r"

  • param string $str
  • param float $percent
  • param string $obfuscateChar
  • param string[] $keepChars
  • psalm-pure
  • return string
public static str_offset_exists(string $str, int $offset, string $encoding = 'UTF-8') : bool
 

Returns whether or not a character exists at an index. Offsets may be negative to count from the last character in the string. Implements part of the ArrayAccess interface.

  • param string $str
  • param int $offset
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return bool
public static str_offset_get(string $str, int $index, string $encoding = 'UTF-8') : string
 

Returns the character at the given index. Offsets may be negative to count from the last character in the string. Implements part of the ArrayAccess interface, and throws an OutOfBoundsException if the index does not exist.

  • param string $str
  • param int<1, max> $index
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • throws \OutOfBoundsException if the positive or negative offset does not exist
  • return string
  • psalm-pure
public static str_pad(string $str, int $pad_length, string $pad_string = ' ', $pad_type = 1STR_PAD_RIGHT, string $encoding = 'UTF-8') : string
 

Pad a UTF-8 string to a given length with another string.

EXAMPLE: UTF8::str_pad('中文空白', 10, '_', STR_PAD_BOTH); // '中文空白'

  • param string $str
  • param int $pad_length
  • param string $pad_string [optional] String to use for padding the input string.
  • param int|string $pad_type [optional] Can be STR_PAD_RIGHT (default), [or string "right"] STR_PAD_LEFT [or string "left"] or STR_PAD_BOTH [or string "both"]
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_pad_both(string $str, int $length, string $pad_str = ' ', string $encoding = 'UTF-8') : string
 

Returns a new string of a given length such that both sides of the string are padded. Alias for "UTF8::str_pad()" with a $pad_type of 'both'.

  • param string $str
  • param int $length
  • param string $pad_str [optional] String used to pad, defaults to space. Default: ' '
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static str_pad_left(string $str, int $length, string $pad_str = ' ', string $encoding = 'UTF-8') : string
 

Returns a new string of a given length such that the beginning of the string is padded. Alias for "UTF8::str_pad()" with a $pad_type of 'left'.

  • param string $str
  • param int $length
  • param string $pad_str [optional] String used to pad, defaults to space. Default: ' '
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static str_pad_right(string $str, int $length, string $pad_str = ' ', string $encoding = 'UTF-8') : string
 

Returns a new string of a given length such that the end of the string is padded. Alias for "UTF8::str_pad()" with a $pad_type of 'right'.

  • param string $str
  • param int $length
  • param string $pad_str [optional] String used to pad, defaults to space. Default: ' '
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static str_repeat(string $str, int $multiplier) : string
 

Repeat a string.

EXAMPLE: UTF8::str_repeat("°~\xf0\x90\x28\xbc", 2); // '°ð(¼°ð(¼'

  • param string $str
  • param int<1, max> $multiplier
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static str_replace( $search, $replace, $subject, ?int $count = NULL)
 

INFO: This is only a wrapper for "str_replace()" -> the original functions is already UTF-8 safe.

Replace all occurrences of the search string with the replacement string

  • see http://php.net/manual/en/function.str-replace.php
  • param string|string[] $search
  • param string|string[] $replace
  • param string|string[] $subject
  • param int|null $count [optional] If passed, this will hold the number of matched and replaced needles.
  • psalm-pure
  • return string|string[]
  • template TStrReplaceSubject
  • phpstan-param TStrReplaceSubject $subject
  • phpstan-return TStrReplaceSubject
  • deprecated please use \str_replace() instead
public static str_replace_beginning(string $str, string $search, string $replacement) : string
 

Replaces $search from the beginning of string with $replacement.

  • param string $str
  • param string $search
  • param string $replacement
  • psalm-pure
  • return string
public static str_replace_ending(string $str, string $search, string $replacement) : string
 

Replaces $search from the ending of string with $replacement.

  • param string $str
  • param string $search
  • param string $replacement
  • psalm-pure
  • return string
public static str_replace_first(string $search, string $replace, string $subject) : string
 

Replace the first "$search"-term with the "$replace"-term.

  • param string $search
  • param string $replace
  • param string $subject
  • psalm-pure
  • return string
  • psalm-suppress InvalidReturnType
public static str_replace_last(string $search, string $replace, string $subject) : string
 

Replace the last "$search"-term with the "$replace"-term.

  • param string $search
  • param string $replace
  • param string $subject
  • psalm-pure
  • return string
  • psalm-suppress InvalidReturnType
public static str_shuffle(string $str, string $encoding = 'UTF-8') : string
 

Shuffles all the characters in the string.

INFO: uses random algorithm which is weak for cryptography purposes

EXAMPLE: UTF8::str_shuffle('fòô bàř fòô'); // 'àòôřb ffòô '

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static str_slice(string $str, int $start, ?int $end = NULL, string $encoding = 'UTF-8')
 

Returns the substring beginning at $start, and up to, but not including the index specified by $end. If $end is omitted, the function extracts the remaining string. If $end is negative, it is computed from the end of the string.

  • param string $str
  • param int $start
  • param int|null $end [optional] Index at which to end extraction. Default: null
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return false|string
public static str_snakeize(string $str, string $encoding = 'UTF-8') : string
 

Convert a string to e.g.: "snake_case"

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static str_sort(string $str, bool $unique = false, bool $desc = false) : string
 

Sort all characters according to code points.

EXAMPLE: UTF8::str_sort(' -ABC-中文空白- '); // ' ---ABC中文白空'

  • param string $str
  • param bool $unique
  • param bool $desc
  • psalm-pure
  • return string
public static str_split( $str, int $length = 1, bool $clean_utf8 = false, bool $try_to_use_mb_functions = true) : array
 

Convert a string to an array of unicode characters.

EXAMPLE: UTF8::str_split('中文空白'); // array('中', '文', '空', '白')

  • param int|string $str
  • param int<1, max> $length [optional] Max character length of each array element.
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param bool $try_to_use_mb_functions [optional] Set to false, if you don't want to use "mb_substr"
  • psalm-pure
  • return list<string>
public static str_split_array(array $input, int $length = 1, bool $clean_utf8 = false, bool $try_to_use_mb_functions = true) : array
 

Convert a string to an array of Unicode characters.

EXAMPLE: UTF8::str_split_array(['中文空白', 'test'], 2); // [['中文', '空白'], ['te', 'st']]

  • param int[]|string[] $input
  • param int<1, max> $length [optional] Max character length of each array element.
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param bool $try_to_use_mb_functions [optional] Set to false, if you don't want to use "mb_substr"
  • psalm-pure
  • return list<list<string>>
public static str_split_pattern(string $str, string $pattern, int $limit = -1) : array
 

Splits the string with the provided regular expression, returning an array of strings. An optional integer $limit will truncate the results.

  • param string $str
  • param string $pattern
  • param int $limit [optional] Maximum number of results to return. Default: -1 === no limit
  • psalm-pure
  • return string[]
public static str_starts_with(string $haystack, string $needle) : bool
 

Check if the string starts with the given substring.

EXAMPLE: UTF8::str_starts_with('ΚόσμεMiddleEnd', 'Κόσμε'); // true UTF8::str_starts_with('ΚόσμεMiddleEnd', 'κόσμε'); // false

  • param string $haystack
  • param string $needle
  • psalm-pure
  • return bool
public static str_starts_with_any(string $str, array $substrings) : bool
 

Returns true if the string begins with any of $substrings, false otherwise.

  • case-sensitive
  • param string $str
  • param scalar[] $substrings
  • psalm-pure
  • return bool <p>Whether or not $str starts with $substring.
public static str_substr_after_first_separator(string $str, string $separator, string $encoding = 'UTF-8') : string
 

Gets the substring after the first occurrence of a separator.

  • param string $str
  • param string $separator
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_substr_after_last_separator(string $str, string $separator, string $encoding = 'UTF-8') : string
 

Gets the substring after the last occurrence of a separator.

  • param string $str
  • param string $separator
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_substr_before_first_separator(string $str, string $separator, string $encoding = 'UTF-8') : string
 

Gets the substring before the first occurrence of a separator.

  • param string $str
  • param string $separator
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_substr_before_last_separator(string $str, string $separator, string $encoding = 'UTF-8') : string
 

Gets the substring before the last occurrence of a separator.

  • param string $str
  • param string $separator
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_substr_first(string $str, string $needle, bool $before_needle = false, string $encoding = 'UTF-8') : string
 

Gets the substring after (or before via "$before_needle") the first occurrence of the "$needle".

  • param string $str
  • param string $needle
  • param bool $before_needle [optional] Default: false
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_substr_last(string $str, string $needle, bool $before_needle = false, string $encoding = 'UTF-8') : string
 

Gets the substring after (or before via "$before_needle") the last occurrence of the "$needle".

  • param string $str
  • param string $needle
  • param bool $before_needle [optional] Default: false
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_surround(string $str, string $substring) : string
 

Surrounds $str with the given substring.

  • param string $str
  • param string $substring
  • psalm-pure
  • return string
  • template T as string
  • template TSub as string
  • phpstan-param T $str
  • phpstan-param TSub $substring
  • phpstan-return (T is non-empty-string ? non-empty-string : (TSub is non-empty-string ? non-empty-string : string))
public static str_titleize(string $str, ?array $ignore = NULL, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false, bool $use_trim_first = true, ?string $word_define_chars = NULL) : string
 

Returns a trimmed string with the first letter of each word capitalized.

Also accepts an array, $ignore, allowing you to list words not to be capitalized.

  • param string $str
  • param string[]|null $ignore [optional] An array of words not to capitalize or null. Default: null
  • param string $encoding [optional] Default: 'UTF-8'
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • param bool $use_trim_first [optional] true === trim the input string, first
  • param string|null $word_define_chars [optional] An string of chars that will be used as whitespace separator === words.
  • psalm-pure
  • return string
public static str_titleize_for_humans(string $str, array $ignore = [], string $encoding = 'UTF-8') : string
 

Returns a trimmed string in proper title case.

Also accepts an array, $ignore, allowing you to list words not to be capitalized.

Adapted from John Gruber's script.

  • see https://gist.github.com/gruber/9f9e8650d68b13ce4d78
  • param string $str
  • param string[] $ignore
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static str_to_binary(string $str)
 

Get a binary representation of a specific string.

EXAPLE: UTF8::str_to_binary('😃'); // '11110000100111111001100010000011'

  • param string $str
  • psalm-pure
  • return false|string
public static str_to_lines(string $str, bool $remove_empty_values = false, ?int $remove_short_values = NULL) : array
 
  • param string $str
  • param bool $remove_empty_values
  • param int|null $remove_short_values
  • psalm-pure
  • return string[]
public static str_to_words(string $str, string $char_list = '', bool $remove_empty_values = false, ?int $remove_short_values = NULL) : array
 

Convert a string into an array of words.

EXAMPLE: UTF8::str_to_words('中文空白 oöäü#s', '#') // array('', '中文空白', ' ', 'oöäü#s', '')

  • param string $str
  • param string $char_list
  • param bool $remove_empty_values
  • param int|null $remove_short_values
  • psalm-pure
  • return list<string>
  • phpstan-return ($remove_empty_values is true ? list : non-empty-list)
public static str_truncate(string $str, int $length, string $substring = '', string $encoding = 'UTF-8') : string
 

Truncates the string to a given length. If $substring is provided, and truncating occurs, the string is further truncated so that the substring may be appended without exceeding the desired length.

  • param string $str
  • param int $length
  • param string $substring [optional] The substring to append if it can fit. Default: ''
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string
public static str_truncate_safe(string $str, int $length, string $substring = '', string $encoding = 'UTF-8', bool $ignore_do_not_split_words_for_one_word = false) : string
 

Truncates the string to a given length, while ensuring that it does not split words. If $substring is provided, and truncating occurs, the string is further truncated so that the substring may be appended without exceeding the desired length.

  • param string $str
  • param int $length
  • param string $substring [optional] The substring to append if it can fit. Default: ''
  • param string $encoding [optional] Default: 'UTF-8'
  • param bool $ignore_do_not_split_words_for_one_word [optional] Default: false
  • psalm-pure
  • return string
public static str_underscored(string $str) : string
 

Returns a lowercase and trimmed string separated by underscores.

Underscores are inserted before uppercase characters (with the exception of the first character of the string), and in place of spaces as well as dashes.

  • param string $str
  • psalm-pure
  • return string
public static str_upper_camelize(string $str, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Returns an UpperCamelCase version of the supplied string. It trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, underscores.

  • param string $str
  • param string $encoding [optional] Default: 'UTF-8'
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
public static str_word_count(string $str, int $format = 0, string $char_list = '')
 

Get the number of words in a specific string.

EXAMPLES: // format: 0 -> return only word count (int) // UTF8::str_word_count('中文空白 öäü abc#c'); // 4 UTF8::str_word_count('中文空白 öäü abc#c', 0, '#'); // 3

// format: 1 -> return words (array) // UTF8::str_word_count('中文空白 öäü abc#c', 1); // array('中文空白', 'öäü', 'abc', 'c') UTF8::str_word_count('中文空白 öäü abc#c', 1, '#'); // array('中文空白', 'öäü', 'abc#c')

// format: 2 -> return words with offset (array) // UTF8::str_word_count('中文空白 öäü ab#c', 2); // array(0 => '中文空白', 5 => 'öäü', 9 => 'abc', 13 => 'c') UTF8::str_word_count('中文空白 öäü ab#c', 2, '#'); // array(0 => '中文空白', 5 => 'öäü', 9 => 'abc#c')

  • param string $str
  • param int $format [optional] 0 => return a number of words (default) 1 => return an array of words 2 => return an array of words with word-offset as key
  • param string $char_list [optional] Additional chars that contains to words and do not start a new word.
  • psalm-pure
  • return int|string[]
  • phpstan-param 0|1|2 $format
  • phpstan-return ($format is 2 ? array<int, string> : ($format is 1 ? list : 0|positive-int))
public static strcasecmp(string $str1, string $str2, string $encoding = 'UTF-8') : int
 

Case-insensitive string comparison.

INFO: Case-insensitive version of UTF8::strcmp()

EXAMPLE: UTF8::strcasecmp("iñtërnâtiôn\nàlizætiøn", "Iñtërnâtiôn\nàlizætiøn"); // 0

  • param string $str1
  • param string $str2
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return int < 0 if str1 is less than str2; > 0 if str1 is greater than str2, 0 if they are equal
public static strcmp(string $str1, string $str2) : int
 

Case-sensitive string comparison.

EXAMPLE: UTF8::strcmp("iñtërnâtiôn\nàlizætiøn", "iñtërnâtiôn\nàlizætiøn"); // 0

  • param string $str1
  • param string $str2
  • psalm-pure
  • return int < 0 if str1 is less than str2 > 0 if str1 is greater than str2 0 if they are equal
public static strcspn(string $str, string $char_list, int $offset = 0, ?int $length = NULL, string $encoding = 'UTF-8') : int
 

Find length of initial segment not matching mask.

  • param string $str
  • param string $char_list
  • param int $offset
  • param int|null $length
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return int
  • phpstan-return 0|positive-int
public static string( $intOrHex) : string
 

Create a UTF-8 string from code points.

INFO: opposite to UTF8::codepoints()

EXAMPLE: UTF8::string(array(246, 228, 252)); // 'öäü'

  • param int|int[]|string|string[] $intOrHex
  • phpstan-param int[]|numeric-string[]|int|numeric-string $intOrHex
  • psalm-pure
  • return string
public static string_has_bom(string $str) : bool
 

Checks if string starts with "BOM" (Byte Order Mark Character) character.

EXAMPLE: UTF8::string_has_bom("\xef\xbb\xbf foobar"); // true

  • param string $str
  • psalm-pure
  • return bool
public static strip_tags(string $str, ?string $allowable_tags = NULL, bool $clean_utf8 = false) : string
 

Strip HTML and PHP tags from a string + clean invalid UTF-8.

EXAMPLE: UTF8::strip_tags("κόσμε\xa0\xa1"); // 'κόσμε'

  • see http://php.net/manual/en/function.strip-tags.php
  • param string $str
  • param string|null $allowable_tags [optional] You can use the optional second parameter to specify tags which should not be stripped.
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return string
public static strip_whitespace(string $str) : string
 

Strip all whitespace characters. This includes tabs and newline characters, as well as multibyte whitespace such as the thin space and ideographic space.

EXAMPLE: UTF8::strip_whitespace(' Ο συγγραφέας '); // 'Οσυγγραφέας'

  • param string $str
  • psalm-pure
  • return string
public static stripos(string $haystack, string $needle, int $offset = 0, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Find the position of the first occurrence of a substring in a string, case-insensitive.

INFO: use UTF8::stripos_in_byte() for the byte-length

EXAMPLE: UTF8::stripos('aσσb', 'ΣΣ'); // 1 (σσ == ΣΣ)

  • see http://php.net/manual/en/function.mb-stripos.php
  • param string $haystack
  • param string $needle
  • param int $offset [optional] The position in haystack to start searching.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|int Return the (int) numeric position of the first occurrence of needle in the haystack string, or false if needle is not found
  • phpstan-return false|0|positive-int
public static stripos_in_byte(string $haystack, string $needle, int $offset = 0)
 

Find the position of the first occurrence of a substring in a string, case-insensitive.

  • param string $haystack
  • param string $needle
  • param int $offset [optional] The search offset. If it is not specified, 0 is used.
  • psalm-pure
  • return false|int
  • phpstan-return false|0|positive-int
public static stristr(string $haystack, string $needle, bool $before_needle = false, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Returns all of haystack starting from and including the first occurrence of needle to the end.

EXAMPLE: $str = 'iñtërnâtiônàlizætiøn'; $search = 'NÂT';

UTF8::stristr($str, $search)); // 'nâtiônàlizætiøn' UTF8::stristr($str, $search, true)); // 'iñtër'

  • param string $haystack
  • param string $needle
  • param bool $before_needle [optional] If TRUE, it returns the part of the haystack before the first occurrence of the needle (excluding the needle).
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|string
public static strlen(string $str, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Get the string length, not the byte-length!

INFO: use UTF8::strwidth() for the char-length

EXAMPLE: UTF8::strlen("Iñtërnâtiôn\xE9àlizætiøn")); // 20

  • see http://php.net/manual/en/function.mb-strlen.php
  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|int <p> The number <strong>(int)</strong> of characters in the string $str having character encoding $encoding. (One multi-byte character counted as +1). Can return false, if e.g. mbstring is not installed and we process invalid chars.
  • phpstan-return false|0|positive-int
public static strlen_in_byte(string $str) : int
 

Get string length in byte.

  • param string $str
  • psalm-pure
  • return int
  • phpstan-return 0|positive-int
public static strnatcasecmp(string $str1, string $str2, string $encoding = 'UTF-8') : int
 

Case-insensitive string comparisons using a "natural order" algorithm.

INFO: natural order version of UTF8::strcasecmp()

EXAMPLES: UTF8::strnatcasecmp('2', '10Hello WORLD 中文空白!'); // -1 UTF8::strcasecmp('2Hello world 中文空白!', '10Hello WORLD 中文空白!'); // 1

UTF8::strnatcasecmp('10Hello world 中文空白!', '2Hello WORLD 中文空白!'); // 1 UTF8::strcasecmp('10Hello world 中文空白!', '2Hello WORLD 中文空白!'); // -1

  • param string $str1
  • param string $str2
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return int < 0 if str1 is less than str2 > 0 if str1 is greater than str2 0 if they are equal
public static strnatcmp(string $str1, string $str2) : int
 

String comparisons using a "natural order" algorithm

INFO: natural order version of UTF8::strcmp()

EXAMPLES: UTF8::strnatcmp('2Hello world 中文空白!', '10Hello WORLD 中文空白!'); // -1 UTF8::strcmp('2Hello world 中文空白!', '10Hello WORLD 中文空白!'); // 1

UTF8::strnatcmp('10Hello world 中文空白!', '2Hello WORLD 中文空白!'); // 1 UTF8::strcmp('10Hello world 中文空白!', '2Hello WORLD 中文空白!'); // -1

  • see http://php.net/manual/en/function.strnatcmp.php
  • param string $str1
  • param string $str2
  • psalm-pure
  • return int < 0 if str1 is less than str2; > 0 if str1 is greater than str2; 0 if they are equal
public static strncasecmp(string $str1, string $str2, int $len, string $encoding = 'UTF-8') : int
 

Case-insensitive string comparison of the first n characters.

EXAMPLE: UTF8::strcasecmp("iñtërnâtiôn\nàlizætiøn321", "iñtërnâtiôn\nàlizætiøn123", 5); // 0

  • see http://php.net/manual/en/function.strncasecmp.php
  • param string $str1
  • param string $str2
  • param int $len
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return int < 0 if str1 is less than str2; > 0 if str1 is greater than str2; 0 if they are equal
public static strncmp(string $str1, string $str2, int $len, string $encoding = 'UTF-8') : int
 

String comparison of the first n characters.

EXAMPLE: UTF8::strncmp("Iñtërnâtiôn\nàlizætiøn321", "Iñtërnâtiôn\nàlizætiøn123", 5); // 0

  • see http://php.net/manual/en/function.strncmp.php
  • param string $str1
  • param string $str2
  • param int $len
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return int < 0 if str1 is less than str2; > 0 if str1 is greater than str2; 0 if they are equal
public static strpbrk(string $haystack, string $char_list)
 

Search a string for any of a set of characters.

EXAMPLE: UTF8::strpbrk('-中文空白-', '白'); // '白-'

  • see http://php.net/manual/en/function.strpbrk.php
  • param string $haystack
  • param string $char_list
  • psalm-pure
  • return false|string
public static strpos(string $haystack, $needle, int $offset = 0, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Find the position of the first occurrence of a substring in a string.

INFO: use UTF8::strpos_in_byte() for the byte-length

EXAMPLE: UTF8::strpos('ABC-ÖÄÜ-中文空白-中文空白', '中'); // 8

  • see http://php.net/manual/en/function.mb-strpos.php
  • param string $haystack
  • param int|string $needle
  • param int $offset [optional] The search offset. If it is not specified, 0 is used.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|int The (int) numeric position of the first occurrence of needle in the haystack string. If needle is not found it returns false.
  • phpstan-return false|0|positive-int
public static strpos_in_byte(string $haystack, string $needle, int $offset = 0)
 

Find the position of the first occurrence of a substring in a string.

  • param string $haystack
  • param string $needle
  • param int $offset [optional] The search offset. If it is not specified, 0 is used.
  • psalm-pure
  • return false|int
  • phpstan-return false|0|positive-int
public static strrchr(string $haystack, string $needle, bool $before_needle = false, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Find the last occurrence of a character in a string within another.

EXAMPLE: UTF8::strrchr('κόσμεκόσμε-äöü', 'κόσμε'); // 'κόσμε-äöü'

  • see http://php.net/manual/en/function.mb-strrchr.php
  • param string $haystack
  • param string $needle
  • param bool $before_needle [optional] Determines which portion of haystack this function returns. If set to true, it returns all of haystack from the beginning to the last occurrence of needle. If set to false, it returns all of haystack from the last occurrence of needle to the end,
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|string
public static strrev(string $str, string $encoding = 'UTF-8') : string
 

Reverses characters order in the string.

EXAMPLE: UTF8::strrev('κ-öäü'); // 'üäö-κ'

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static strrichr(string $haystack, string $needle, bool $before_needle = false, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Find the last occurrence of a character in a string within another, case-insensitive.

EXAMPLE: UTF8::strrichr('Aκόσμεκόσμε-äöü', 'aκόσμε'); // 'Aκόσμεκόσμε-äöü'

  • see http://php.net/manual/en/function.mb-strrichr.php
  • param string $haystack
  • param string $needle
  • param bool $before_needle [optional] Determines which portion of haystack this function returns. If set to true, it returns all of haystack from the beginning to the last occurrence of needle. If set to false, it returns all of haystack from the last occurrence of needle to the end,
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|string
public static strripos(string $haystack, $needle, int $offset = 0, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Find the position of the last occurrence of a substring in a string, case-insensitive.

EXAMPLE: UTF8::strripos('ABC-ÖÄÜ-中文空白-中文空白', '中'); // 13

  • param string $haystack
  • param int|string $needle
  • param int $offset [optional] Number of characters to ignore in the beginning or end.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|int
public static strripos_in_byte(string $haystack, string $needle, int $offset = 0)
 

Finds position of last occurrence of a string within another, case-insensitive.

  • param string $haystack
  • param string $needle
  • param int $offset [optional] The position in haystack to start searching.
  • psalm-pure
  • return false|int
public static strrpos(string $haystack, $needle, int $offset = 0, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Find the position of the last occurrence of a substring in a string.

EXAMPLE: UTF8::strrpos('ABC-ÖÄÜ-中文空白-中文空白', '中'); // 13

  • see http://php.net/manual/en/function.mb-strrpos.php
  • param string $haystack
  • param int|string $needle
  • param int $offset [optional] May be specified to begin searching an arbitrary number of characters into the string. Negative values will stop searching at an arbitrary point prior to the end of the string.
  • param string $encoding [optional] Set the charset.
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|int
public static strrpos_in_byte(string $haystack, string $needle, int $offset = 0)
 

Find the position of the last occurrence of a substring in a string.

  • param string $haystack
  • param string $needle
  • param int $offset [optional] May be specified to begin searching an arbitrary number of characters into the string. Negative values will stop searching at an arbitrary point prior to the end of the string.
  • psalm-pure
  • return false|int
public static strspn(string $str, string $mask, int $offset = 0, ?int $length = NULL, string $encoding = 'UTF-8')
 

Finds the length of the initial segment of a string consisting entirely of characters contained within a given mask.

EXAMPLE: UTF8::strspn('iñtërnâtiônàlizætiøn', 'itñ'); // '3'

  • param string $str
  • param string $mask
  • param int $offset [optional]
  • param int|null $length [optional]
  • param string $encoding [optional] Set the charset.
  • psalm-pure
  • return false|int
public static strstr(string $haystack, string $needle, bool $before_needle = false, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Returns part of haystack string from the first occurrence of needle to the end of haystack.

EXAMPLE: $str = 'iñtërnâtiônàlizætiøn'; $search = 'nât';

UTF8::strstr($str, $search)); // 'nâtiônàlizætiøn' UTF8::strstr($str, $search, true)); // 'iñtër'

  • param string $haystack
  • param string $needle
  • param bool $before_needle [optional] If TRUE, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle).
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|string
public static strstr_in_byte(string $haystack, string $needle, bool $before_needle = false)
 

Finds first occurrence of a string within another.

  • param string $haystack
  • param string $needle
  • param bool $before_needle [optional] Determines which portion of haystack this function returns. If set to true, it returns all of haystack from the beginning to the first occurrence of needle. If set to false, it returns all of haystack from the first occurrence of needle to the end,
  • psalm-pure
  • return false|string
public static strtocasefold(string $str, bool $full = true, bool $clean_utf8 = false, string $encoding = 'UTF-8', ?string $lang = NULL, bool $lower = true) : string
 

Unicode transformation for case-less matching.

EXAMPLE: UTF8::strtocasefold('ǰ◌̱'); // 'ǰ◌̱'

  • see http://unicode.org/reports/tr21/tr21-5.html
  • param string $str
  • param bool $full [optional] <p> <b>true</b>, replace full case folding chars (default)<br> <b>false</b>, use only limited static array [UTF8::$COMMON_CASE_FOLD]
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string $encoding [optional] Set the charset.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $lower [optional] Use lowercase string, otherwise use uppercase string. PS: uppercase is for some languages better ...
  • psalm-pure
  • return string
public static strtolower( $str, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Make a string lowercase.

EXAMPLE: UTF8::strtolower('DÉJÀ Σσς Iıİi'); // 'déjà σσς iıii'

  • see http://php.net/manual/en/function.mb-strtolower.php
  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
public static strtoupper( $str, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Make a string uppercase.

EXAMPLE: UTF8::strtoupper('Déjà Σσς Iıİi'); // 'DÉJÀ ΣΣΣ IIİI'

  • see http://php.net/manual/en/function.mb-strtoupper.php
  • param string $str
  • param string $encoding [optional] Set the charset.
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
public static strtr(string $str, $from, $to = '') : string
 

Translate characters or replace sub-strings.

EXAMPLE: $array = [ 'Hello' => '○●◎', '中文空白' => 'earth', ]; UTF8::strtr('Hello 中文空白', $array); // '○●◎ earth'

  • see http://php.net/manual/en/function.strtr.php
  • param string $str
  • param string|string[] $from
  • param string|string[] $to [optional] The string being translated to to.
  • psalm-pure
  • return string
public static strwidth(string $str, string $encoding = 'UTF-8', bool $clean_utf8 = false) : int
 

Return the width of a string.

INFO: use UTF8::strlen() for the byte-length

EXAMPLE: UTF8::strwidth("Iñtërnâtiôn\xE9àlizætiøn")); // 21

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return int
  • phpstan-return 0|positive-int
public static substr(string $str, int $offset = 0, ?int $length = NULL, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Get part of a string.

EXAMPLE: UTF8::substr('中文空白', 1, 2); // '文空'

  • see http://php.net/manual/en/function.mb-substr.php
  • param string $str
  • param int $offset
  • param int|null $length [optional] The maximum length of the returned string.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|string The portion of str specified by the offset and length parameters.If str is shorter than offset characters long, FALSE will be returned.
public static substr_compare(string $str1, string $str2, int $offset = 0, ?int $length = NULL, bool $case_insensitivity = false, string $encoding = 'UTF-8') : int
 

Binary-safe comparison of two strings from an offset, up to a length of characters.

EXAMPLE: UTF8::substr_compare("○●◎\r", '●◎', 0, 2); // -1 UTF8::substr_compare("○●◎\r", '◎●', 1, 2); // 1 UTF8::substr_compare("○●◎\r", '●◎', 1, 2); // 0

  • param string $str1
  • param string $str2
  • param int $offset [optional] The start position for the comparison. If negative, it starts counting from the end of the string.
  • param int|null $length [optional] The length of the comparison. The default value is the largest of the length of the str compared to the length of main_str less the offset.
  • param bool $case_insensitivity [optional] If case_insensitivity is TRUE, comparison is case insensitive.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return int < 0 if str1 is less than str2; > 0 if str1 is greater than str2, 0 if they are equal
public static substr_count(string $haystack, string $needle, int $offset = 0, ?int $length = NULL, string $encoding = 'UTF-8', bool $clean_utf8 = false)
 

Count the number of substring occurrences.

EXAMPLE: UTF8::substr_count('中文空白', '文空', 1, 2); // 1

  • see http://php.net/manual/en/function.substr-count.php
  • param string $haystack
  • param string $needle
  • param int $offset [optional] The offset where to start counting.
  • param int|null $length [optional] The maximum length after the specified offset to search for the substring. It outputs a warning if the offset plus the length is greater than the haystack length.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return false|int
public static substr_count_in_byte(string $haystack, string $needle, int $offset = 0, ?int $length = NULL)
 

Count the number of substring occurrences.

  • param string $haystack
  • param string $needle
  • param int $offset [optional] The offset where to start counting
  • param int|null $length [optional] The maximum length after the specified offset to search for the substring. It outputs a warning if the offset plus the length is greater than the haystack length.
  • psalm-pure
  • return false|int
public static substr_count_simple(string $str, string $substring, bool $case_sensitive = true, string $encoding = 'UTF-8') : int
 

Returns the number of occurrences of $substring in the given string.

By default, the comparison is case-sensitive, but can be made insensitive by setting $case_sensitive to false.

  • param string $str
  • param string $substring
  • param bool $case_sensitive [optional] Whether or not to enforce case-sensitivity. Default: true
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return int
  • phpstan-return 0|positive-int
public static substr_ileft(string $haystack, string $needle) : string
 

Removes a prefix ($needle) from the beginning of the string ($haystack), case-insensitive.

EXMAPLE: UTF8::substr_ileft('ΚόσμεMiddleEnd', 'Κόσμε'); // 'MiddleEnd' UTF8::substr_ileft('ΚόσμεMiddleEnd', 'κόσμε'); // 'MiddleEnd'

  • param string $haystack
  • param string $needle
  • psalm-pure
  • return string
public static substr_in_byte(string $str, int $offset = 0, ?int $length = NULL)
 

Get part of a string process in bytes.

  • param string $str
  • param int $offset
  • param int|null $length [optional] The maximum length of the returned string.
  • psalm-pure
  • return false|string
public static substr_iright(string $haystack, string $needle) : string
 

Removes a suffix ($needle) from the end of the string ($haystack), case-insensitive.

EXAMPLE: UTF8::substr_iright('BeginMiddleΚόσμε', 'Κόσμε'); // 'BeginMiddle' UTF8::substr_iright('BeginMiddleΚόσμε', 'κόσμε'); // 'BeginMiddle'

  • param string $haystack
  • param string $needle
  • psalm-pure
  • return string
public static substr_left(string $haystack, string $needle) : string
 

Removes a prefix ($needle) from the beginning of the string ($haystack).

EXAMPLE: UTF8::substr_left('ΚόσμεMiddleEnd', 'Κόσμε'); // 'MiddleEnd' UTF8::substr_left('ΚόσμεMiddleEnd', 'κόσμε'); // 'ΚόσμεMiddleEnd'

  • param string $haystack
  • param string $needle
  • psalm-pure
  • return string
public static substr_replace( $str, $replacement, $offset, $length = NULL, string $encoding = 'UTF-8')
 

Replace text within a portion of a string.

EXAMPLE: UTF8::substr_replace(array('Iñtërnâtiônàlizætiøn', 'foo'), 'æ', 1); // array('Iæñtërnâtiônàlizætiøn', 'fæoo')

source: https://gist.github.com/stemar/8287074

  • param string|string[] $str
  • param string|string[] $replacement
  • param int|int[] $offset
  • param int|int[]|null $length [optional] If given and is positive, it represents the length of the portion of string which is to be replaced. If it is negative, it represents the number of characters from the end of string at which to stop replacing. If it is not given, then it will default to strlen( string ); i.e. end the replacing at the end of string. Of course, if length is zero then this function will have the effect of inserting replacement into string at the given start offset.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string|string[]
  • template TSubstrReplace string|string[]
  • phpstan-param TSubstrReplace $str
  • phpstan-return TSubstrReplace
public static substr_right(string $haystack, string $needle, string $encoding = 'UTF-8') : string
 

Removes a suffix ($needle) from the end of the string ($haystack).

EXAMPLE: UTF8::substr_right('BeginMiddleΚόσμε', 'Κόσμε'); // 'BeginMiddle' UTF8::substr_right('BeginMiddleΚόσμε', 'κόσμε'); // 'BeginMiddleΚόσμε'

  • param string $haystack
  • param string $needle
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static swapCase(string $str, string $encoding = 'UTF-8', bool $clean_utf8 = false) : string
 

Returns a case swapped version of the string.

EXAMPLE: UTF8::swapCase('déJÀ σσς iıII'); // 'DÉjà ΣΣΣ IIii'

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return string
public static symfony_polyfill_used() : bool
 

Checks whether symfony-polyfills are used.

  • psalm-pure
  • return bool
  • internal
public static tabs_to_spaces(string $str, int $tab_length = 4) : string
 
  • param string $str
  • param int $tab_length
  • psalm-pure
  • return string
public static titlecase(string $str, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Converts the first character of each word in the string to uppercase and all other chars to lowercase.

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string <p>A string with all characters of $str being title-cased.
public static to_ascii(string $str, string $unknown = '?', bool $strict = false) : string
 

Convert a string into ASCII.

EXAMPLE: UTF8::to_ascii('déjà σσς iıii'); // 'deja sss iiii'

  • param string $str
  • param string $unknown [optional] Character use if character unknown. (default is ?)
  • param bool $strict [optional] Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance
  • psalm-pure
  • return string
public static to_boolean( $str) : bool
 
  • param bool|float|int|string $str
  • psalm-pure
  • return bool
public static to_filename(string $str, bool $use_transliterate = false, string $fallback_char = '-') : string
 

Convert given string to safe filename (and keep string case).

  • param string $str
  • param bool $use_transliterate No transliteration, conversion etc. is done by default - unsafe characters are simply replaced with hyphen.
  • param string $fallback_char
  • psalm-pure
  • return string
public static to_int(string $str)
 

Returns the given string as an integer, or null if the string isn't numeric.

  • param string $str
  • psalm-pure
  • return int|null
public static to_iso8859( $str)
 

Convert a string into "ISO-8859"-encoding (Latin-1).

EXAMPLE: UTF8::to_utf8(UTF8::to_iso8859(' -ABC-中文空白- ')); // ' -ABC-????- '

  • param string|string[] $str
  • psalm-pure
  • return string|string[]
  • template TToIso8859 as string|string[]
  • phpstan-param TToIso8859 $str
  • phpstan-return (TToIso8859 is string ? string : string[])
public static to_string( $input)
 

Returns the given input as string, or null if the input isn't int|float|string and do not implement the "__toString()" method.

  • param float|int|object|string|null $input
  • psalm-pure
  • return string|null
public static to_utf8( $str, bool $decode_html_entity_to_utf8 = false)
 

This function leaves UTF-8 characters alone, while converting almost all non-UTF8 to UTF8.

EXAMPLE: UTF8::to_utf8(["cat"]); // array('cat')

  • param string|string[] $str
  • param bool $decode_html_entity_to_utf8
  • psalm-pure
  • return string|string[]
  • template TToUtf8 as string|string[]
  • phpstan-param TToUtf8 $str
  • phpstan-return (TToUtf8 is string ? string : string[])
public static to_utf8_string(string $str, bool $decode_html_entity_to_utf8 = false) : string
 

This function leaves UTF-8 characters alone, while converting almost all non-UTF8 to UTF8.

EXAMPLE: UTF8::to_utf8_string("cat"); // 'cat'

  • param string $str
  • param bool $decode_html_entity_to_utf8
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static trim(string $str = '', ?string $chars = NULL) : string
 

Strip whitespace or other characters from the beginning and end of a UTF-8 string.

INFO: This is slower then "trim()"

We can only use the original-function, if we use <= 7-Bit in the string / chars but the check for ASCII (7-Bit) cost more time, then we can safe here.

EXAMPLE: UTF8::trim(' -ABC-中文空白- '); // '-ABC-中文空白-'

  • param string $str
  • param string|null $chars [optional] Optional characters to be stripped
  • psalm-pure
  • return string
public static ucfirst(string $str, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Makes string's first char uppercase.

EXAMPLE: UTF8::ucfirst('ñtërnâtiônàlizætiøn foo'); // 'Ñtërnâtiônàlizætiøn foo'

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
public static ucwords(string $str, array $exceptions = [], string $char_list = '', string $encoding = 'UTF-8', bool $clean_utf8 = false) : string
 

Uppercase for all words in the string.

EXAMPLE: UTF8::ucwords('iñt ërn âTi ônà liz æti øn'); // 'Iñt Ërn ÂTi Ônà Liz Æti Øn'

  • param string $str
  • param string[] $exceptions [optional] Exclusion for some words.
  • param string $char_list [optional] Additional chars that contains to words and do not start a new word.
  • param string $encoding [optional] Set the charset.
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return string
public static urldecode(string $str, bool $multi_decode = true) : string
 

Multi decode HTML entity + fix urlencoded-win1252-chars.

EXAMPLE: UTF8::urldecode('tes%20öäü%20ítest+test'); // 'tes öäü ítest test'

e.g: 'test+test' => 'test test' 'Düsseldorf' => 'Düsseldorf' 'D%FCsseldorf' => 'Düsseldorf' 'Düsseldorf' => 'Düsseldorf' 'D%26%23xFC%3Bsseldorf' => 'Düsseldorf' 'Düsseldorf' => 'Düsseldorf' 'D%C3%BCsseldorf' => 'Düsseldorf' 'D%C3%83%C2%BCsseldorf' => 'Düsseldorf' 'D%25C3%2583%25C2%25BCsseldorf' => 'Düsseldorf'

  • param string $str
  • param bool $multi_decode
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static utf8_decode(string $str, bool $keep_utf8_chars = false) : string
 

Decodes a UTF-8 string to ISO-8859-1.

EXAMPLE: UTF8::encode('UTF-8', UTF8::utf8_decode('-ABC-中文空白-')); // '-ABC-????-'

  • param string $str
  • param bool $keep_utf8_chars
  • psalm-pure
  • return string
public static utf8_encode(string $str) : string
 

Encodes an ISO-8859-1 string to UTF-8.

EXAMPLE: UTF8::utf8_decode(UTF8::utf8_encode('-ABC-中文空白-')); // '-ABC-中文空白-'

  • param string $str
  • psalm-pure
  • return string
public static whitespace_table() : array
 

Returns an array with all utf8 whitespace characters.

  • see http://www.bogofilter.org/pipermail/bogofilter/2003-March/001889.html
  • psalm-pure
  • return string[] An array with all known whitespace characters as values and the type of whitespace as keys as defined in above URL
public static words_limit(string $str, int $limit = 100, string $str_add_on = '…') : string
 

Limit the number of words in a string.

EXAMPLE: UTF8::words_limit('fòô bàř fòô', 2, ''); // 'fòô bàř'

  • param string $str
  • param int<1, max> $limit
  • param string $str_add_on
  • psalm-pure
  • return string
public static wordwrap(string $str, int $width = 75, string $break = ' ', bool $cut = false) : string
 

Wraps a string to a given number of characters

EXAMPLE: UTF8::wordwrap('Iñtërnâtiônàlizætiøn', 2, '', true)); // 'Iñtërnâtiônàlizætiøn'

  • see http://php.net/manual/en/function.wordwrap.php
  • param string $str
  • param int<1, max> $width [optional] The column width.
  • param string $break [optional] The line is broken using the optional break parameter.
  • param bool $cut [optional] If the cut is set to true, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart.
  • psalm-pure
  • return string
public static wordwrap_per_line(string $str, int $width = 75, string $break = ' ', bool $cut = false, bool $add_final_break = true, ?string $delimiter = NULL) : string
 

Line-Wrap the string after $limit, but split the string by "$delimiter" before ... ... so that we wrap the per line.

  • param string $str
  • param int<1, max> $width [optional] The column width.
  • param string $break [optional] The line is broken using the optional break parameter.
  • param bool $cut [optional] If the cut is set to true, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart.
  • param bool $add_final_break [optional] <p> If this flag is true, then the method will add a $break at the end of the result string.
  • param non-empty-string|null $delimiter [optional] You can change the default behavior, where we split the string by newline.
  • psalm-pure
  • return string
public static ws() : array
 

Returns an array of Unicode White Space characters.

  • psalm-pure
  • return string[]
Properties
private static $BOM = ['' => 3, '' => 6, '��' => 4, ' þÿ' => 6, '��' => 4, 'ÿþ ' => 6, '��' => 2, 'þÿ' => 4, '��' => 2, 'ÿþ' => 4]
 

Bom => Byte-Length

  • var array<string,int>
private static $BROKEN_UTF8_FIX = ['‚' => '‚', '„' => '„', '…' => '…', '‡' => '‡', '‰' => '‰', '‹' => '‹', '‘' => '‘', '’' => '’', '“' => '“', '•' => '•', '–' => '–', '—' => '—', 'â„¢' => '™', '›' => '›', '€' => '€', '€' => '€', '‚' => '‚', 'ƒ' => 'ƒ', '„' => '„', '…' => '…', '†' => '†', '‡' => '‡', 'ˆ' => 'ˆ', '‰' => '‰', 'Š' => 'Š', '‹' => '‹', 'Œ' => 'Œ', 'Ž' => 'Ž', '‘' => '‘', '’' => '’', '“' => '“', '”' => '”', '•' => '•', '–' => '–', '—' => '—', '˜' => '˜', 'Â' => 'Â', 'Æ’' => 'ƒ', 'Ã' => 'Ã', 'Ä' => 'Ä', 'Ã…' => 'Å', 'Æ' => 'Æ', 'Ç' => 'Ç', 'ˆ' => 'ˆ', 'È' => 'È', 'É' => 'É', 'Ê' => 'Ê', 'Ë' => 'Ë', 'Å’' => 'Œ', 'ÃŒ' => 'Ì', 'Ž' => 'Ž', 'ÃŽ' => 'Î', 'Ñ' => 'Ñ', 'Ã’' => 'Ò', 'Ó' => 'Ó', 'â€' => '”', 'Ô' => 'Ô', 'Õ' => 'Õ', 'Ö' => 'Ö', '×' => '×', 'Ëœ' => '˜', 'Ø' => 'Ø', 'Ù' => 'Ù', 'Å¡' => 'š', 'Ú' => 'Ú', 'Û' => 'Û', 'Å“' => 'œ', 'Ãœ' => 'Ü', 'ž' => 'ž', 'Þ' => 'Þ', 'Ÿ' => 'Ÿ', 'ß' => 'ß', '¡' => '¡', 'á' => 'á', '¢' => '¢', 'â' => 'â', '£' => '£', 'ã' => 'ã', '¤' => '¤', 'ä' => 'ä', 'Â¥' => '¥', 'Ã¥' => 'å', '¦' => '¦', 'æ' => 'æ', '§' => '§', 'ç' => 'ç', '¨' => '¨', 'è' => 'è', '©' => '©', 'é' => 'é', 'ª' => 'ª', 'ê' => 'ê', '«' => '«', 'ë' => 'ë', '¬' => '¬', 'ì' => 'ì', '®' => '®', 'î' => 'î', '¯' => '¯', 'ï' => 'ï', '°' => '°', 'ð' => 'ð', '±' => '±', 'ñ' => 'ñ', '²' => '²', 'ò' => 'ò', '³' => '³', 'ó' => 'ó', '´' => '´', 'ô' => 'ô', 'µ' => 'µ', 'õ' => 'õ', '¶' => '¶', 'ö' => 'ö', '·' => '·', '÷' => '÷', '¸' => '¸', 'ø' => 'ø', '¹' => '¹', 'ù' => 'ù', 'º' => 'º', 'ú' => 'ú', '»' => '»', 'û' => 'û', '¼' => '¼', 'ü' => 'ü', '½' => '½', 'ý' => 'ý', '¾' => '¾', 'þ' => 'þ', '¿' => '¿', 'ÿ' => 'ÿ', 'À' => 'À']
 
  • var string[]|null
  • phpstan-var array<string, string>|null
private static $CHR = ['', '', '', '', '', '', '', '', '', ' ', ' ', ' ', ' ', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ' ', '!', '"', '#', '$', '%', '&', ''', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�']
 
  • var string[]|null
  • phpstan-var array<int, string>|null
private static $COMMON_CASE_FOLD = ['upper' => ['µ', 'ſ', 'ͅ', 'ς', 'ẞ', 'ϐ', 'ϑ', 'ϕ', 'ϖ', 'ϰ', 'ϱ', 'ϵ', 'ẛ', 'ι'], 'lower' => ['μ', 's', 'ι', 'σ', 'ß', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', 'ṡ', 'ι']]
 
  • var array
  • phpstan-var array{upper: string[], lower: string[]}
private static $EMOJI = NULL
 
  • var string[]|null
  • phpstan-var array<string, string>|null
private static $EMOJI_KEYS_CACHE = NULL
 
  • var string[]|null
  • phpstan-var array|null
private static $EMOJI_KEYS_REVERSIBLE_CACHE = NULL
 
  • var string[]|null
  • phpstan-var array|null
private static $EMOJI_VALUES_CACHE = NULL
 
  • var string[]|null
  • phpstan-var array|null
private static $ENCODINGS = NULL
 
  • var string[]|null
  • phpstan-var array|null
private static $INTL_TRANSLITERATOR_LIST = NULL
 
  • var string[]|null
  • phpstan-var array<int ,string>|null
private static $ORD = NULL
 
  • var int[]|null
  • phpstan-var array<string ,int>|null
private static $SUPPORT = []
 
  • var array
  • phpstan-var array<string, mixed>
private static $WHITESPACE = ['', 9 => ' ', 10 => ' ', 11 => ' ', 13 => ' ', 32 => ' ', 160 => ' ', 5760 => ' ', 6158 => '᠎', 8192 => ' ', 8193 => ' ', 8194 => ' ', 8195 => ' ', 8196 => ' ', 8197 => ' ', 8198 => ' ', 8199 => ' ', 8200 => ' ', 8201 => ' ', 8202 => ' ', 8232 => '
', 8233 => '
', 8239 => ' ', 8287 => ' ', 65440 => 'ᅠ', 12288 => ' ']
 

Numeric code point => UTF-8 Character

  • var array<int,string>
private static $WHITESPACE_TABLE = ['SPACE' => ' ', 'NO-BREAK SPACE' => ' ', 'OGHAM SPACE MARK' => ' ', 'EN QUAD' => ' ', 'EM QUAD' => ' ', 'EN SPACE' => ' ', 'EM SPACE' => ' ', 'THREE-PER-EM SPACE' => ' ', 'FOUR-PER-EM SPACE' => ' ', 'SIX-PER-EM SPACE' => ' ', 'FIGURE SPACE' => ' ', 'PUNCTUATION SPACE' => ' ', 'THIN SPACE' => ' ', 'HAIR SPACE' => ' ', 'LINE SEPARATOR' => '
', 'PARAGRAPH SEPARATOR' => '
', 'ZERO WIDTH SPACE' => '​', 'NARROW NO-BREAK SPACE' => ' ', 'MEDIUM MATHEMATICAL SPACE' => ' ', 'IDEOGRAPHIC SPACE' => ' ', 'HALFWIDTH HANGUL FILLER' => 'ᅠ']
 
  • var array<string,string>
private static $WIN1252_TO_UTF8 = NULL
 
  • var string[]|null
  • phpstan-var array<int, string>|null
Methods
private static fixStrCaseHelper(string $str, bool $use_lowercase = false, bool $use_full_case_fold = false)
 
  • param string $str
  • param bool $use_lowercase
  • param bool $use_full_case_fold
  • psalm-pure
  • return string
private static getData(string $file)
 

get data from "/data/*.php"

  • param string $file
  • psalm-pure
  • return array<array-key,mixed>
private static initEmojiData()
 
  • psalm-pure
  • return true|null
private static is_utf8_string(string $str, bool $strict = false)
 

Checks whether the passed string contains only byte sequences that are valid UTF-8 characters.

EXAMPLE: UTF8::is_utf8_string('Iñtërnâtiônàlizætiøn']); // true // UTF8::is_utf8_string("Iñtërnâtiônàlizætiøn\xA0\xA1"); // false

  • see http://hsivonen.iki.fi/php-utf8/
  • param string $str
  • param bool $strict
  • psalm-pure
  • return bool
private static mbstring_overloaded() : bool
 

Checks whether mbstring "overloaded" is active on the server.

  • psalm-pure
  • return bool
private static reduce_string_array(array $strings, bool $remove_empty_values, ?int $remove_short_values = NULL)
 
  • param string[] $strings
  • param bool $remove_empty_values
  • param int|null $remove_short_values
  • psalm-pure
  • return list<string>
private static rxClass(string $s, string $class = '')
 

rxClass

  • param string $s
  • param string $class
  • return string
  • psalm-pure
private static str_capitalize_name_helper(string $names, string $delimiter, string $encoding = 'UTF-8')
 

Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius").

  • param string $names
  • param string $delimiter
  • param string $encoding
  • phpstan-param non-empty-string $delimiter
  • psalm-pure
  • return string
private static strtonatfold(string $str)
 

Generic case-sensitive transformation for collation matching.

  • param string $str
  • psalm-pure
  • return string|null
private static to_utf8_convert_helper( $input)
 
  • param int|string $input
  • psalm-pure
  • return string
private static urldecode_unicode_helper(string $str)
 
  • param string $str
  • psalm-pure
  • return string
Properties
private static $BOM = ['' => 3, '' => 6, '��' => 4, ' þÿ' => 6, '��' => 4, 'ÿþ ' => 6, '��' => 2, 'þÿ' => 4, '��' => 2, 'ÿþ' => 4]
 

Bom => Byte-Length

  • var array<string,int>
private static $BROKEN_UTF8_FIX = ['‚' => '‚', '„' => '„', '…' => '…', '‡' => '‡', '‰' => '‰', '‹' => '‹', '‘' => '‘', '’' => '’', '“' => '“', '•' => '•', '–' => '–', '—' => '—', 'â„¢' => '™', '›' => '›', '€' => '€', '€' => '€', '‚' => '‚', 'ƒ' => 'ƒ', '„' => '„', '…' => '…', '†' => '†', '‡' => '‡', 'ˆ' => 'ˆ', '‰' => '‰', 'Š' => 'Š', '‹' => '‹', 'Œ' => 'Œ', 'Ž' => 'Ž', '‘' => '‘', '’' => '’', '“' => '“', '”' => '”', '•' => '•', '–' => '–', '—' => '—', '˜' => '˜', 'Â' => 'Â', 'Æ’' => 'ƒ', 'Ã' => 'Ã', 'Ä' => 'Ä', 'Ã…' => 'Å', 'Æ' => 'Æ', 'Ç' => 'Ç', 'ˆ' => 'ˆ', 'È' => 'È', 'É' => 'É', 'Ê' => 'Ê', 'Ë' => 'Ë', 'Å’' => 'Œ', 'ÃŒ' => 'Ì', 'Ž' => 'Ž', 'ÃŽ' => 'Î', 'Ñ' => 'Ñ', 'Ã’' => 'Ò', 'Ó' => 'Ó', 'â€' => '”', 'Ô' => 'Ô', 'Õ' => 'Õ', 'Ö' => 'Ö', '×' => '×', 'Ëœ' => '˜', 'Ø' => 'Ø', 'Ù' => 'Ù', 'Å¡' => 'š', 'Ú' => 'Ú', 'Û' => 'Û', 'Å“' => 'œ', 'Ãœ' => 'Ü', 'ž' => 'ž', 'Þ' => 'Þ', 'Ÿ' => 'Ÿ', 'ß' => 'ß', '¡' => '¡', 'á' => 'á', '¢' => '¢', 'â' => 'â', '£' => '£', 'ã' => 'ã', '¤' => '¤', 'ä' => 'ä', 'Â¥' => '¥', 'Ã¥' => 'å', '¦' => '¦', 'æ' => 'æ', '§' => '§', 'ç' => 'ç', '¨' => '¨', 'è' => 'è', '©' => '©', 'é' => 'é', 'ª' => 'ª', 'ê' => 'ê', '«' => '«', 'ë' => 'ë', '¬' => '¬', 'ì' => 'ì', '®' => '®', 'î' => 'î', '¯' => '¯', 'ï' => 'ï', '°' => '°', 'ð' => 'ð', '±' => '±', 'ñ' => 'ñ', '²' => '²', 'ò' => 'ò', '³' => '³', 'ó' => 'ó', '´' => '´', 'ô' => 'ô', 'µ' => 'µ', 'õ' => 'õ', '¶' => '¶', 'ö' => 'ö', '·' => '·', '÷' => '÷', '¸' => '¸', 'ø' => 'ø', '¹' => '¹', 'ù' => 'ù', 'º' => 'º', 'ú' => 'ú', '»' => '»', 'û' => 'û', '¼' => '¼', 'ü' => 'ü', '½' => '½', 'ý' => 'ý', '¾' => '¾', 'þ' => 'þ', '¿' => '¿', 'ÿ' => 'ÿ', 'À' => 'À']
 
  • var string[]|null
  • phpstan-var array<string, string>|null
private static $CHR = ['', '', '', '', '', '', '', '', '', ' ', ' ', ' ', ' ', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ' ', '!', '"', '#', '$', '%', '&', ''', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�', '�']
 
  • var string[]|null
  • phpstan-var array<int, string>|null
private static $COMMON_CASE_FOLD = ['upper' => ['µ', 'ſ', 'ͅ', 'ς', 'ẞ', 'ϐ', 'ϑ', 'ϕ', 'ϖ', 'ϰ', 'ϱ', 'ϵ', 'ẛ', 'ι'], 'lower' => ['μ', 's', 'ι', 'σ', 'ß', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', 'ṡ', 'ι']]
 
  • var array
  • phpstan-var array{upper: string[], lower: string[]}
private static $EMOJI = NULL
 
  • var string[]|null
  • phpstan-var array<string, string>|null
private static $EMOJI_KEYS_CACHE = NULL
 
  • var string[]|null
  • phpstan-var array|null
private static $EMOJI_KEYS_REVERSIBLE_CACHE = NULL
 
  • var string[]|null
  • phpstan-var array|null
private static $EMOJI_VALUES_CACHE = NULL
 
  • var string[]|null
  • phpstan-var array|null
private static $ENCODINGS = NULL
 
  • var string[]|null
  • phpstan-var array|null
private static $INTL_TRANSLITERATOR_LIST = NULL
 
  • var string[]|null
  • phpstan-var array<int ,string>|null
private static $ORD = NULL
 
  • var int[]|null
  • phpstan-var array<string ,int>|null
private static $SUPPORT = []
 
  • var array
  • phpstan-var array<string, mixed>
private static $WHITESPACE = ['', 9 => ' ', 10 => ' ', 11 => ' ', 13 => ' ', 32 => ' ', 160 => ' ', 5760 => ' ', 6158 => '᠎', 8192 => ' ', 8193 => ' ', 8194 => ' ', 8195 => ' ', 8196 => ' ', 8197 => ' ', 8198 => ' ', 8199 => ' ', 8200 => ' ', 8201 => ' ', 8202 => ' ', 8232 => '
', 8233 => '
', 8239 => ' ', 8287 => ' ', 65440 => 'ᅠ', 12288 => ' ']
 

Numeric code point => UTF-8 Character

  • var array<int,string>
private static $WHITESPACE_TABLE = ['SPACE' => ' ', 'NO-BREAK SPACE' => ' ', 'OGHAM SPACE MARK' => ' ', 'EN QUAD' => ' ', 'EM QUAD' => ' ', 'EN SPACE' => ' ', 'EM SPACE' => ' ', 'THREE-PER-EM SPACE' => ' ', 'FOUR-PER-EM SPACE' => ' ', 'SIX-PER-EM SPACE' => ' ', 'FIGURE SPACE' => ' ', 'PUNCTUATION SPACE' => ' ', 'THIN SPACE' => ' ', 'HAIR SPACE' => ' ', 'LINE SEPARATOR' => '
', 'PARAGRAPH SEPARATOR' => '
', 'ZERO WIDTH SPACE' => '​', 'NARROW NO-BREAK SPACE' => ' ', 'MEDIUM MATHEMATICAL SPACE' => ' ', 'IDEOGRAPHIC SPACE' => ' ', 'HALFWIDTH HANGUL FILLER' => 'ᅠ']
 
  • var array<string,string>
private static $WIN1252_TO_UTF8 = NULL
 
  • var string[]|null
  • phpstan-var array<int, string>|null
Methods
public static access(string $str, int $pos, string $encoding = 'UTF-8') : string
 

Return the character at the specified position: $str[1] like functionality.

EXAMPLE: UTF8::access('fòô', 1); // 'ò'

  • param string $str
  • param int $pos
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static add_bom_to_string(string $str) : string
 

Prepends UTF-8 BOM character to the string and returns the whole string.

INFO: If BOM already existed there, the Input string is returned.

EXAMPLE: UTF8::add_bom_to_string('fòô'); // "\xEF\xBB\xBF" . 'fòô'

  • param string $str
  • psalm-pure
  • return non-empty-string
public static array_change_key_case(array $array, int $case = 0CASE_LOWER, string $encoding = 'UTF-8') : array
 

Changes all keys in an array.

  • param array<string,mixed> $array
  • param int $case [optional] Either CASE_UPPER or CASE_LOWER (default)
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string[]
public static between(string $str, string $start, string $end, int $offset = 0, string $encoding = 'UTF-8') : string
 

Returns the substring between $start and $end, if found, or an empty string. An optional offset may be supplied from which to begin the search for the start string.

  • param string $str
  • param string $start
  • param string $end
  • param int $offset [optional] Index from which to begin the search. Default: 0
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static binary_to_str( $bin) : string
 

Convert binary into a string.

INFO: opposite to UTF8::str_to_binary()

EXAMPLE: UTF8::binary_to_str('11110000100111111001100010000011'); // '😃'

  • param string $bin 1|0
  • psalm-pure
  • return string
public static bom() : string
 

Returns the UTF-8 Byte Order Mark Character.

INFO: take a look at UTF8::$bom for e.g. UTF-16 and UTF-32 BOM values

EXAMPLE: UTF8::bom(); // "\xEF\xBB\xBF"

  • psalm-pure
  • return non-empty-string
public static callback( $callback, string $str) : array
 
  • alias of UTF8::chr_map()
  • param callable string $callback
  • param string $str
  • psalm-pure
  • return string[]
  • see \UTF8::chr_map()
public static char_at(string $str, int $index, string $encoding = 'UTF-8') : string
 

Returns the character at $index, with indexes starting at 0.

  • param string $str
  • param int<1, max> $index
  • param string $encoding [optional] Default is UTF-8
  • psalm-pure
  • return string <p>The character at $index.
public static chars(string $str) : array
 

Returns an array consisting of the characters in the string.

  • param string $str
  • psalm-pure
  • return string[]
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-list : list)
public static checkForSupport()
 

This method will auto-detect your server environment for UTF-8 support.

  • return true|null
  • internal
public static chr( $code_point, string $encoding = 'UTF-8')
 

Generates a UTF-8 encoded character from the given code point.

INFO: opposite to UTF8::ord()

EXAMPLE: UTF8::chr(0x2603); // '☃'

  • param int $code_point
  • param string $encoding [optional] Default is UTF-8
  • psalm-pure
  • return string|null
public static chr_map( $callback, string $str) : array
 

Applies callback to all characters of a string.

EXAMPLE: UTF8::chr_map([UTF8::class, 'strtolower'], 'Κόσμε'); // ['κ','ό', 'σ', 'μ', 'ε']

  • param callable string $callback The callback function.
  • param string $str
  • psalm-pure
  • return string[]
public static chr_size_list(string $str) : array
 

Generates an array of byte length of each character of a Unicode string.

1 byte => U+0000 - U+007F 2 byte => U+0080 - U+07FF 3 byte => U+0800 - U+FFFF 4 byte => U+10000 - U+10FFFF

EXAMPLE: UTF8::chr_size_list('中文空白-test'); // [3, 3, 3, 3, 1, 1, 1, 1, 1]

  • param string $str
  • psalm-pure
  • return int[]
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-list<1|2|3|4> : list<1|2|3|4>)
public static chr_to_decimal(string $char) : int
 

Get a decimal code representation of a specific character.

INFO: opposite to UTF8::decimal_to_chr()

EXAMPLE: UTF8::chr_to_decimal('§'); // 0xa7

  • param string $char
  • psalm-pure
  • return int
public static chr_to_hex( $char, string $prefix = 'U+') : string
 

Get hexadecimal code point (U+xxxx) of a UTF-8 encoded character.

EXAMPLE: UTF8::chr_to_hex('§'); // U+00a7

  • param int|string $char
  • param string $prefix [optional]
  • psalm-pure
  • return string
public static chunk_split(string $str, int $chunk_length = 76, string $end = ' ') : string
 

Splits a string into smaller chunks and multiple lines, using the specified line ending character.

EXAMPLE: UTF8::chunk_split('ABC-ÖÄÜ-中文空白-κόσμε', 3); // "ABC\r\n-ÖÄ\r\nÜ-中\r\n文空白\r\n-κό\r\nσμε"

  • param string $str
  • param int<1, max> $chunk_length [optional] The maximum character length of a chunk.
  • param string $end [optional] The character(s) to be inserted at the end of each chunk.
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static clean(string $str, bool $remove_bom = false, bool $normalize_whitespace = false, bool $normalize_msword = false, bool $keep_non_breaking_space = false, bool $replace_diamond_question_mark = false, bool $remove_invisible_characters = true, bool $remove_invisible_characters_url_encoded = false) : string
 

Accepts a string and removes all non-UTF-8 characters from it + extras if needed.

EXAMPLE: UTF8::clean("\xEF\xBB\xBF„Abcdef\xc2\xa0\x20…” — 😃 - Düsseldorf", true, true); // '„Abcdef  …” — 😃 - Düsseldorf'

  • param string $str
  • param bool $remove_bom [optional] Set to true, if you need to remove UTF-BOM.
  • param bool $normalize_whitespace [optional] Set to true, if you need to normalize the whitespace.
  • param bool $normalize_msword [optional] Set to true, if you need to normalize MS Word chars e.g.: "…" => "..."
  • param bool $keep_non_breaking_space [optional] <p>Set to true, to keep non-breaking-spaces, in combination with $normalize_whitespace
  • param bool $replace_diamond_question_mark [optional] Set to true, if you need to remove diamond question mark e.g.: "�"
  • param bool $remove_invisible_characters [optional] Set to false, if you not want to remove invisible characters e.g.: "\0"
  • param bool $remove_invisible_characters_url_encoded [optional] Set to true, if you not want to remove invisible url encoded characters e.g.: "%0B" WARNING: maybe contains false-positives e.g. aa%0Baa -> aaaa.
  • psalm-pure
  • return string
public static cleanup( $str) : string
 

Clean-up a string and show only printable UTF-8 chars at the end + fix UTF-8 encoding.

EXAMPLE: UTF8::cleanup("\xEF\xBB\xBF„Abcdef\xc2\xa0\x20…” — 😃 - Düsseldorf", true, true); // '„Abcdef  …” — 😃 - Düsseldorf'

  • param string $str
  • psalm-pure
  • return string
public static codepoints( $arg, bool $use_u_style = false) : array
 

Accepts a string or an array of chars and returns an array of Unicode code points.

INFO: opposite to UTF8::string()

EXAMPLE: UTF8::codepoints('κöñ'); // array(954, 246, 241) // ... OR ... UTF8::codepoints('κöñ', true); // array('U+03ba', 'U+00f6', 'U+00f1')

  • param string|string[] $arg
  • param bool $use_u_style
  • psalm-pure
  • return int[]|string[] <p> The array of code points:<br> int[] for $u_style === false string[] for $u_style === true
  • template T as string|string[]
  • phpstan-param T $arg
  • phpstan-return (T is non-empty-string ? ($use_u_style is true ? non-empty-list : non-empty-list) : ($use_u_style is true ? list : list))
public static collapse_whitespace(string $str) : string
 

Trims the string and replaces consecutive whitespace characters with a single space. This includes tabs and newline characters, as well as multibyte whitespace such as the thin space and ideographic space.

  • param string $str
  • psalm-pure
  • return string <p>A string with trimmed $str and condensed whitespace.
public static count_chars(string $str, bool $clean_utf8 = false, bool $try_to_use_mb_functions = true) : array
 

Returns count of characters used in a string.

EXAMPLE: UTF8::count_chars('κaκbκc'); // array('κ' => 3, 'a' => 1, 'b' => 1, 'c' => 1)

  • param string $str
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param bool $try_to_use_mb_functions [optional] Set to false, if you don't want to use
  • psalm-pure
  • return int[]
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-array<string, int> : array<string, int>)
public static css_identifier(string $str = '', array $filter = [' ' => '-', '/' => '-', '[' => '', ']' => ''], bool $strip_tags = false, bool $strtolower = true) : string
 

Create a valid CSS identifier for e.g. "class"- or "id"-attributes.

EXAMPLE: UTF8::css_identifier('123foo/bar!!!'); // _23foo-bar

copy&past from https://github.com/drupal/core/blob/8.8.x/lib/Drupal/Component/Utility/Html.php#L95

  • param string $str
  • param string[] $filter
  • param bool $strip_tags
  • param bool $strtolower
  • psalm-pure
  • return string
  • phpstan-param array<string,string> $filter
public static css_stripe_media_queries(string $str) : string
 

Remove css media-queries.

  • param string $str
  • psalm-pure
  • return string
public static ctype_loaded() : bool
 

Checks whether ctype is available on the server.

  • psalm-pure
  • return bool
  • internal
public static decimal_to_chr( $int) : string
 

Converts an int value into a UTF-8 character.

INFO: opposite to UTF8::string()

EXAMPLE: UTF8::decimal_to_chr(931); // 'Σ'

  • param int|string $int
  • phpstan-param int|numeric-string $int
  • psalm-pure
  • return string
public static decode_mimeheader( $str, string $encoding = 'UTF-8')
 

Decodes a MIME header field

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return false|string
public static emoji_decode(string $str, bool $use_reversible_string_mappings = false) : string
 

Decodes a string which was encoded by "UTF8::emoji_encode()".

INFO: opposite to UTF8::emoji_encode()

EXAMPLE: UTF8::emoji_decode('foo CHARACTER_OGRE', false); // 'foo 👹' // UTF8::emoji_decode('foo -PORTABLE_UTF8-308095726-627590803-8FTU_ELBATROP-', true); // 'foo 👹'

  • param string $str
  • param bool $use_reversible_string_mappings [optional] When TRUE, we se a reversible string mapping between "emoji_encode" and "emoji_decode".
  • psalm-pure
  • return string
public static emoji_encode(string $str, bool $use_reversible_string_mappings = false) : string
 

Encode a string with emoji chars into a non-emoji string.

INFO: opposite to UTF8::emoji_decode()

EXAMPLE: UTF8::emoji_encode('foo 👹', false)); // 'foo CHARACTER_OGRE' // UTF8::emoji_encode('foo 👹', true)); // 'foo -PORTABLE_UTF8-308095726-627590803-8FTU_ELBATROP-'

  • param string $str
  • param bool $use_reversible_string_mappings [optional] when TRUE, we use a reversible string mapping between "emoji_encode" and "emoji_decode"
  • psalm-pure
  • return string
public static emoji_from_country_code(string $country_code_iso_3166_1) : string
 

Convert any two-letter country code (ISO 3166-1) to the corresponding Emoji.

  • see https://en.wikipedia.org/wiki/ISO_3166-1
  • param string $country_code_iso_3166_1
  • return string
public static encode(string $to_encoding, string $str, bool $auto_detect_the_from_encoding = true, string $from_encoding = '') : string
 

Encode a string with a new charset-encoding.

INFO: This function will also try to fix broken / double encoding, so you can call this function also on a UTF-8 string and you don't mess up the string.

EXAMPLE: UTF8::encode('ISO-8859-1', '-ABC-中文空白-'); // '-ABC-????-' // UTF8::encode('UTF-8', '-ABC-中文空白-'); // '-ABC-中文空白-' // UTF8::encode('HTML', '-ABC-中文空白-'); // '-ABC-中文空白-' // UTF8::encode('BASE64', '-ABC-中文空白-'); // 'LUFCQy3kuK3mlofnqbrnmb0t'

  • param string $to_encoding
  • param string $str
  • param bool $auto_detect_the_from_encoding [optional] Force the new encoding (we try to fix broken / double encoding for UTF-8) otherwise we auto-detect the current string-encoding
  • param string $from_encoding [optional] e.g. 'UTF-16', 'UTF-8', 'ISO-8859-1', etc. A empty string will trigger the autodetect anyway.
  • psalm-pure
  • return string
  • psalm-suppress InvalidReturnStatement
public static encode_mimeheader(string $str, string $from_charset = 'UTF-8', string $to_charset = 'UTF-8', string $transfer_encoding = 'Q', string $linefeed = ' ', int $indent = 76)
 
  • param string $str
  • param string $from_charset [optional] Set the input charset.
  • param string $to_charset [optional] Set the output charset.
  • param string $transfer_encoding [optional] Set the transfer encoding.
  • param string $linefeed [optional] Set the used linefeed.
  • param int<1, max> $indent [optional] Set the max length indent.
  • psalm-pure
  • return false|string
public static extract_text(string $str, string $search = '', ?int $length = NULL, string $replacer_for_skipped_text = '…', string $encoding = 'UTF-8') : string
 

Create an extract from a sentence, so if the search-string was found, it tries to center in the output.

  • param string $str
  • param string $search
  • param int|null $length [optional] Default: null === text->length / 2
  • param string $replacer_for_skipped_text [optional] Default: …
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static file_get_contents(string $filename, bool $use_include_path = false, $context = NULL, ?int $offset = NULL, ?int $max_length = NULL, int $timeout = 10, bool $convert_to_utf8 = true, string $from_encoding = '')
 

Reads entire file into a string.

EXAMPLE: UTF8::file_get_contents('utf16le.txt'); // ...

WARNING: Do not use UTF-8 Option ($convert_to_utf8) for binary files (e.g.: images) !!!

  • see http://php.net/manual/en/function.file-get-contents.php
  • param string $filename
  • param bool $use_include_path [optional] Prior to PHP 5, this parameter is called use_include_path and is a bool. As of PHP 5 the FILE_USE_INCLUDE_PATH can be used to trigger include path search.
  • param resource|null $context [optional] A valid context resource created with stream_context_create. If you don't need to use a custom context, you can skip this parameter by &null;.
  • param int|null $offset [optional] The offset where the reading starts.
  • param int<0, max>|null $max_length [optional] Maximum length of data read. The default is to read until end of file is reached.
  • param int $timeout
  • param bool $convert_to_utf8 WARNING!!! Maybe you can't use this option for some files, because they used non default utf-8 chars. Binary files like images or pdf will not be converted.
  • param string $from_encoding [optional] e.g. 'UTF-16', 'UTF-8', 'ISO-8859-1', etc. A empty string will trigger the autodetect anyway.
  • psalm-pure
  • return false|string
public static file_has_bom(string $file_path) : bool
 

Checks if a file starts with BOM (Byte Order Mark) character.

EXAMPLE: UTF8::file_has_bom('utf8_with_bom.txt'); // true

  • param string $file_path
  • throws \RuntimeException if file_get_contents() returned false
  • return bool
  • psalm-pure
public static filter( $var, int $normalization_form = 16Normalizer::NFC, string $leading_combining = '◌')
 

Normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.

EXAMPLE: UTF8::filter(array("\xE9", 'à', 'a')); // array('é', 'à', 'a')

  • param array|object|string $var
  • param int $normalization_form
  • param string $leading_combining
  • psalm-pure
  • return mixed
  • template TFilter
  • phpstan-param TFilter $var
  • phpstan-return TFilter
public static filter_input(int $type, string $variable_name, int $filter = 516FILTER_DEFAULT, $options = NULL)
 

"filter_input()"-wrapper with normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.

Gets a specific external variable by name and optionally filters it.

EXAMPLE: // _GET['foo'] = 'bar'; UTF8::filter_input(INPUT_GET, 'foo', FILTER_UNSAFE_RAW)); // 'bar'

  • see http://php.net/manual/en/function.filter-input.php
  • param int $type
  • param string $variable_name
  • param int $filter [optional] The ID of the filter to apply. The manual page lists the available filters.
  • param int|int[]|null $options [optional] Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array.
  • psalm-pure
  • return mixed
public static filter_input_array(int $type, $definition = NULL, bool $add_empty = true)
 

"filter_input_array()"-wrapper with normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.

Gets external variables and optionally filters them.

EXAMPLE: // _GET['foo'] = 'bar'; UTF8::filter_input_array(INPUT_GET, array('foo' => 'FILTER_UNSAFE_RAW')); // array('bar')

  • see http://php.net/manual/en/function.filter-input-array.php
  • param int $type
  • param array<string,mixed>|null $definition [optional] An array defining the arguments. A valid key is a string containing a variable name and a valid value is either a filter type, or an array optionally specifying the filter, flags and options. If the value is an array, valid keys are filter which specifies the filter type, flags which specifies any flags that apply to the filter, and options which specifies any options that apply to the filter. See the example below for a better understanding.
  • param bool $add_empty [optional] Add missing keys as NULL to the return value.
  • psalm-pure
  • return array<string,mixed>|false|null
public static filter_var( $variable, int $filter = 516FILTER_DEFAULT, $options = 0)
 

"filter_var()"-wrapper with normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.

Filters a variable with a specified filter.

EXAMPLE: UTF8::filter_var('-ABC-中文空白-', FILTER_VALIDATE_URL); // false

  • see http://php.net/manual/en/function.filter-var.php
  • param float|int|string|null $variable
  • param int $filter [optional] The ID of the filter to apply. The manual page lists the available filters.
  • param int|int[] $options [optional] <p> Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array. For the "callback" filter, callable type should be passed. The callback must accept one argument, the value to be filtered, and return the value after filtering/sanitizing it. </p> <p> <code> // for filters that accept options, use this format $options = array( 'options' => array( 'default' => 3, // value to return if the filter fails // other options here 'min_range' => 0 ), 'flags' => FILTER_FLAG_ALLOW_OCTAL, ); $var = filter_var('0755', FILTER_VALIDATE_INT, $options); // for filter that only accept flags, you can pass them directly $var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); // for filter that only accept flags, you can also pass as an array $var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)); // callback validate filter function foo($value) { // Expected format: Surname, GivenNames if (strpos($value, ", ") === false) return false; list($surname, $givennames) = explode(", ", $value, 2); $empty = (empty($surname) || empty($givennames)); $notstrings = (!is_string($surname) || !is_string($givennames)); if ($empty || $notstrings) { return false; } else { return $value; } } $var = filter_var('Doe, Jane Sue', FILTER_CALLBACK, array('options' => 'foo'));
  • psalm-pure
  • return mixed
public static filter_var_array(array $data, $definition = 0, bool $add_empty = true)
 

"filter_var_array()"-wrapper with normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed.

Gets multiple variables and optionally filters them.

EXAMPLE: $filters = [ 'name' => ['filter' => FILTER_CALLBACK, 'options' => [UTF8::class, 'ucwords']], 'age' => ['filter' => FILTER_VALIDATE_INT, 'options' => ['min_range' => 1, 'max_range' => 120]], 'email' => FILTER_VALIDATE_EMAIL, ];

$data = [ 'name' => 'κόσμε', 'age' => '18', 'email' => 'foo@bar.de' ];

UTF8::filter_var_array($data, $filters, true); // ['name' => 'Κόσμε', 'age' => 18, 'email' => 'foo@bar.de']

  • see http://php.net/manual/en/function.filter-var-array.php
  • param array<string,mixed> $data
  • param array<string,mixed>|int $definition [optional] An array defining the arguments. A valid key is a string containing a variable name and a valid value is either a filter type, or an array optionally specifying the filter, flags and options. If the value is an array, valid keys are filter which specifies the filter type, flags which specifies any flags that apply to the filter, and options which specifies any options that apply to the filter. See the example below for a better understanding.
  • param bool $add_empty [optional] Add missing keys as NULL to the return value.
  • psalm-pure
  • return array<string,mixed>|false|null
public static finfo_loaded() : bool
 

Checks whether finfo is available on the server.

  • psalm-pure
  • return bool
  • internal
public static first_char(string $str, int $n = 1, string $encoding = 'UTF-8') : string
 

Returns the first $n characters of the string.

  • param string $str
  • param int<1, max> $n
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static fits_inside(string $str, int $box_size) : bool
 

Check if the number of Unicode characters isn't greater than the specified integer.

EXAMPLE: UTF8::fits_inside('κόσμε', 6); // false

  • param string $str the original string to be checked
  • param int $box_size the size in number of chars to be checked against string
  • psalm-pure
  • return bool <p><strong>TRUE</strong> if string is less than or equal to $box_size, FALSE otherwise.
public static fix_simple_utf8(string $str) : string
 

Try to fix simple broken UTF-8 strings.

INFO: Take a look at "UTF8::fix_utf8()" if you need a more advanced fix for broken UTF-8 strings.

EXAMPLE: UTF8::fix_simple_utf8('Düsseldorf'); // 'Düsseldorf'

If you received an UTF-8 string that was converted from Windows-1252 as it was ISO-8859-1 (ignoring Windows-1252 chars from 80 to 9F) use this function to fix it. See: http://en.wikipedia.org/wiki/Windows-1252

  • param string $str
  • psalm-pure
  • return string
public static fix_utf8( $str)
 

Fix a double (or multiple) encoded UTF8 string.

EXAMPLE: UTF8::fix_utf8('Fédération'); // 'Fédération'

  • param string|string[] $str you can use a string or an array of strings
  • psalm-pure
  • return string|string[]
  • template TFixUtf8 as string|string[]
  • phpstan-param TFixUtf8 $str
  • phpstan-return TFixUtf8
private static fixStrCaseHelper(string $str, bool $use_lowercase = false, bool $use_full_case_fold = false)
 
  • param string $str
  • param bool $use_lowercase
  • param bool $use_full_case_fold
  • psalm-pure
  • return string
public static get_file_type(string $str, array $fallback = ['ext' => NULL, 'mime' => 'application/octet-stream', 'type' => NULL]) : array
 

Warning: this method only works for some file-types (png, jpg) if you need more supported types, please use e.g. "finfo"

  • param string $str
  • param array{ext: null|string, mime: null|string, type: null|string} $fallback
  • return array{ext: null|string, mime: null|string, type: null|string}
  • psalm-pure
public static get_random_string(int $length, string $possible_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', string $encoding = 'UTF-8') : string
 
  • param int<1, max> $length
  • param string $possible_chars [optional] Characters string for the random selection.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • return string
  • template T as string
  • phpstan-param T $possible_chars
  • phpstan-return (T is non-empty-string ? non-empty-string : '')
public static get_unique_string( $extra_entropy = '', bool $use_md5 = true) : string
 
  • param int|string $extra_entropy [optional] Extra entropy via a string or int value.
  • param bool $use_md5 [optional] Return the unique identifier as md5-hash? Default: true
  • return non-empty-string
public static getCharDirection(string $char) : string
 

Get character of a specific character.

EXAMPLE: UTF8::getCharDirection('ا'); // 'RTL'

  • param string $char
  • psalm-pure
  • return string
private static getData(string $file)
 

get data from "/data/*.php"

  • param string $file
  • psalm-pure
  • return array<array-key,mixed>
public static getSupportInfo(?string $key = NULL)
 

Check for php-support.

  • param string|null $key
  • psalm-pure
  • return mixed Return the full support-"array", if $key === null return bool-value, if $key is used and available otherwise return null
public static getUrlParamFromArray(string $param, array $data)
 

Get data from an array via array like string.

EXAMPLE: $array['foo'][123] = 'lall'; UTF8::getUrlParamFromArray('foo[123]', $array); // 'lall'

  • param array<array-key,mixed> $data
  • return mixed
public static has_lowercase(string $str) : bool
 

Returns true if the string contains a lower case char, false otherwise.

  • param string $str
  • psalm-pure
  • return bool
public static has_uppercase(string $str) : bool
 

Returns true if the string contains an upper case char, false otherwise.

  • param string $str
  • psalm-pure
  • return bool
public static has_whitespace(string $str) : bool
 

Returns true if the string contains whitespace, false otherwise.

  • param string $str
  • psalm-pure
  • return bool
public static hex_to_chr(string $hexdec)
 

Converts a hexadecimal value into a UTF-8 character.

INFO: opposite to UTF8::chr_to_hex()

EXAMPLE: UTF8::hex_to_chr('U+00a7'); // '§'

  • param string $hexdec
  • psalm-pure
  • return string
public static hex_to_int( $hexdec)
 

Converts hexadecimal U+xxxx code point representation to integer.

INFO: opposite to UTF8::int_to_hex()

EXAMPLE: UTF8::hex_to_int('U+00f1'); // 241

  • param string $hexdec
  • psalm-pure
  • return false|int
public static html_encode(string $str, bool $keep_ascii_chars = false, string $encoding = 'UTF-8') : string
 

Converts a UTF-8 string to a series of HTML numbered entities.

INFO: opposite to UTF8::html_decode()

EXAMPLE: UTF8::html_encode('中文空白'); // '中文空白'

  • param string $str
  • param bool $keep_ascii_chars [optional] Keep ASCII chars.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static html_entity_decode(string $str, ?int $flags = NULL, string $encoding = 'UTF-8') : string
 

UTF-8 version of html_entity_decode()

The reason we are not using html_entity_decode() by itself is because while it is not technically correct to leave out the semicolon at the end of an entity most browsers will still interpret the entity correctly. html_entity_decode() does not convert entities without semicolons, so we are left with our own little solution here. Bummer.

Convert all HTML entities to their applicable characters.

INFO: opposite to UTF8::html_encode()

EXAMPLE: UTF8::html_entity_decode('中文空白'); // '中文空白'

  • see http://php.net/manual/en/function.html-entity-decode.php
  • param string $str
  • param int|null $flags [optional] A bitmask of one or more of the following flags, which specify how to handle quotes and which document type to use. The default is ENT_COMPAT | ENT_HTML401.
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static html_escape(string $str, string $encoding = 'UTF-8') : string
 

Create a escape html version of the string via "UTF8::htmlspecialchars()".

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
public static html_stripe_empty_tags(string $str) : string
 

Remove empty html-tag.

e.g.:

  • param string $str
  • psalm-pure
  • return string
public static htmlentities(string $str, int $flags = 2ENT_COMPAT, string $encoding = 'UTF-8', bool $double_encode = true) : string
 

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
  • 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.
  • param string $encoding [optional] 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
public static htmlspecialchars(string $str, int $flags = 2ENT_COMPAT, string $encoding = 'UTF-8', bool $double_encode = true) : string
 

Convert only special characters to HTML entities: UTF-8 version of htmlspecialchars()

INFO: Take a look at "UTF8::htmlentities()"

EXAMPLE: UTF8::htmlspecialchars('<白-öäü>'); // '<白-öäü>'

  • see http://php.net/manual/en/function.htmlspecialchars.php
  • param string $str
  • 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.
  • param string $encoding [optional] Defines encoding used in conversion.
  • 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
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static iconv_loaded() : bool
 

Checks whether iconv is available on the server.

  • psalm-pure
  • return bool
  • internal
private static initEmojiData()
 
  • psalm-pure
  • return true|null
public static int_to_hex(int $int, string $prefix = 'U+') : string
 

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
  • param string $prefix [optional]
  • psalm-pure
  • return string the code point, or empty string on failure
public static intl_loaded() : bool
 

Checks whether intl is available on the server.

  • psalm-pure
  • return bool
  • internal
public static intlChar_loaded() : bool
 

Checks whether intl-char is available on the server.

  • psalm-pure
  • return bool
  • internal
public static is_alpha(string $str) : bool
 

Returns true if the string contains only alphabetic chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only alphabetic chars.
public static is_alphanumeric(string $str) : bool
 

Returns true if the string contains only alphabetic and numeric chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only alphanumeric chars.
public static is_ascii(string $str) : bool
 

Checks if a string is 7 bit ASCII.

EXAMPLE: UTF8::is_ascii('白'); // false

  • param string $str
  • psalm-pure
  • return bool
public static is_base64( $str, bool $empty_string_is_valid = false) : bool
 

Returns true if the string is base64 encoded, false otherwise.

EXAMPLE: UTF8::is_base64('4KSu4KWL4KSo4KS/4KSa'); // true

  • param string|null $str
  • param bool $empty_string_is_valid [optional] Is an empty string valid base64 or not?
  • psalm-pure
  • return bool <p>Whether or not $str is base64 encoded.
public static is_binary( $input, bool $strict = false) : bool
 

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 is_binary_file( $file) : bool
 

Check if the file is binary.

EXAMPLE: UTF8::is_binary('./utf32.txt'); // true

  • param string $file
  • return bool
public static is_blank(string $str) : bool
 

Returns true if the string contains only whitespace chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only whitespace characters.
public static is_bom( $str) : bool
 

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
  • psalm-pure
  • return bool <p><strong>true</strong> if the $utf8_chr is Byte Order Mark, false otherwise.
public static is_empty( $str) : bool
 

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 array<array-key,mixed>|float|int|string $str
  • psalm-pure
  • return bool <p>Whether or not $str is empty().
public static is_hexadecimal(string $str) : bool
 

Returns true if the string contains only hexadecimal chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only hexadecimal chars.
public static is_html(string $str) : bool
 

Check if the string contains any HTML tags.

EXAMPLE: UTF8::is_html('lall'); // true

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains html elements.
public static is_json(string $str, bool $only_array_or_object_results_are_valid = true) : bool
 

Try to check if "$str" is a JSON-string.

EXAMPLE: UTF8::is_json('{"array":[1,"¥","ä"]}'); // true

  • param string $str
  • param bool $only_array_or_object_results_are_valid [optional] Only array and objects are valid json results.
  • return bool <p>Whether or not the $str is in JSON format.
public static is_lowercase(string $str) : bool
 
  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only lowercase chars.
public static is_printable(string $str, bool $ignore_control_characters = false) : bool
 

Returns true if the string contains only printable (non-invisible) chars, false otherwise.

  • param string $str
  • param bool $ignore_control_characters [optional] Ignore control characters like [LRM] or [LSEP].
  • psalm-pure
  • return bool <p>Whether or not $str contains only printable (non-invisible) chars.
public static is_punctuation(string $str) : bool
 

Returns true if the string contains only punctuation chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only punctuation chars.
public static is_serialized(string $str) : bool
 

Returns true if the string is serialized, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str is serialized.
public static is_uppercase(string $str) : bool
 

Returns true if the string contains only lower case chars, false otherwise.

  • param string $str
  • psalm-pure
  • return bool <p>Whether or not $str contains only lower case characters.
public static is_url(string $url, bool $disallow_localhost = false) : bool
 

Check if $url is an correct url.

  • param string $url
  • param bool $disallow_localhost
  • psalm-pure
  • return bool
public static is_utf16( $str, bool $check_if_string_is_binary = true)
 

Check if the string is UTF-16.

EXAMPLE: UTF8::is_utf16(file_get_contents('utf-16-le.txt')); // 1 // UTF8::is_utf16(file_get_contents('utf-16-be.txt')); // 2 // UTF8::is_utf16(file_get_contents('utf-8.txt')); // false

  • param string $str
  • param bool $check_if_string_is_binary
  • psalm-pure
  • return false|int false if is't not UTF-16, 1 for UTF-16LE, 2 for UTF-16BE
public static is_utf32( $str, bool $check_if_string_is_binary = true)
 

Check if the string is UTF-32.

EXAMPLE: UTF8::is_utf32(file_get_contents('utf-32-le.txt')); // 1 // UTF8::is_utf32(file_get_contents('utf-32-be.txt')); // 2 // UTF8::is_utf32(file_get_contents('utf-8.txt')); // false

  • param string $str
  • param bool $check_if_string_is_binary
  • psalm-pure
  • return false|int false if is't not UTF-32, 1 for UTF-32LE, 2 for UTF-32BE
public static is_utf8( $str, bool $strict = false) : bool
 

Checks whether the passed input contains only byte sequences that appear valid UTF-8.

EXAMPLE: UTF8::is_utf8(['Iñtërnâtiônàlizætiøn', 'foo']); // true // UTF8::is_utf8(["Iñtërnâtiônàlizætiøn\xA0\xA1", 'bar']); // false

  • param int|string|string[]|null $str
  • param bool $strict
  • psalm-pure
  • return bool
private static is_utf8_string(string $str, bool $strict = false)
 

Checks whether the passed string contains only byte sequences that are valid UTF-8 characters.

EXAMPLE: UTF8::is_utf8_string('Iñtërnâtiônàlizætiøn']); // true // UTF8::is_utf8_string("Iñtërnâtiônàlizætiøn\xA0\xA1"); // false

  • see http://hsivonen.iki.fi/php-utf8/
  • param string $str
  • param bool $strict
  • psalm-pure
  • return bool
public static json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
 

(PHP 5 >= 5.2.0, PECL json >= 1.2.0) Decodes a JSON string

EXAMPLE: UTF8::json_decode('[1,"¥","ä"]'); // array(1, '¥', 'ä')

  • see http://php.net/manual/en/function.json-decode.php
  • param string $json
  • param bool $assoc [optional] When TRUE, returned objects will be converted into associative arrays.
  • param int $depth [optional] User specified recursion depth.
  • param int $options [optional] Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)
  • psalm-pure
  • return mixed
public static json_encode( $value, int $options = 0, int $depth = 512)
 

(PHP 5 >= 5.2.0, PECL json >= 1.2.0) Returns the JSON representation of a value.

EXAMPLE: UTF8::json_encode(array(1, '¥', 'ä')); // '[1,"¥","ä"]'

  • see http://php.net/manual/en/function.json-encode.php
  • param mixed $value
  • param int $options [optional] Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE. The behaviour of these constants is described on the JSON constants page.
  • param int $depth [optional] Set the maximum depth. Must be greater than zero.
  • psalm-pure
  • return false|string
public static json_loaded() : bool
 

Checks whether JSON is available on the server.

  • psalm-pure
  • return bool
  • internal
public static lcfirst(string $str, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Makes string's first char lowercase.

EXAMPLE: UTF8::lcfirst('ÑTËRNÂTIÔNÀLIZÆTIØN'); // ñTËRNÂTIÔNÀLIZÆTIØN

  • param string $str
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
public static lcwords(string $str, array $exceptions = [], string $char_list = '', string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Lowercase for all words in the string.

  • param string $str
  • param string[] $exceptions [optional] Exclusion for some words.
  • param string $char_list [optional] Additional chars that contains to words and do not start a new word.
  • param string $encoding [optional] Set the charset.
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
public static levenshtein(string $str1, string $str2, int $insertionCost = 1, int $replacementCost = 1, int $deletionCost = 1) : int
 

Calculate Levenshtein distance between two strings.

For better performance, in a real application with a single input string matched against many strings from a database, you will probably want to pre- encode the input only once and use \levenshtein().

Source: https://github.com/KEINOS/mb_levenshtein

  • see https://www.php.net/manual/en/function.levenshtein
  • param string $str1
  • param string $str2
  • param int $insertionCost [optional] Defines the cost of insertion.
  • param int $replacementCost [optional] Defines the cost of replacement.
  • param int $deletionCost [optional] Defines the cost of deletion.
  • return int
public static ltrim(string $str = '', ?string $chars = NULL) : string
 

Strip whitespace or other characters from the beginning of a UTF-8 string.

EXAMPLE: UTF8::ltrim(' 中文空白  '); // '中文空白  '

  • param string $str
  • param string|null $chars
  • psalm-pure
  • return string the string with unwanted characters stripped from the left
public static max( $arg)
 

Returns the UTF-8 character with the maximum code point in the given data.

EXAMPLE: UTF8::max('abc-äöü-中文空白'); // 'ø'

  • param string|string[] $arg
  • psalm-pure
  • return string|null the character with the highest code point than others, returns null on failure or empty input
public static max_chr_width(string $str) : int
 

Calculates and returns the maximum number of bytes taken by any UTF-8 encoded character in the given string.

EXAMPLE: UTF8::max_chr_width('Intërnâtiônàlizætiøn'); // 2

  • param string $str
  • psalm-pure
  • return int
  • phpstan-return 0|1|2|3|4
public static mbstring_loaded() : bool
 

Checks whether mbstring is available on the server.

  • psalm-pure
  • return bool
  • internal
private static mbstring_overloaded() : bool
 

Checks whether mbstring "overloaded" is active on the server.

  • psalm-pure
  • return bool
public static min( $arg)
 

Returns the UTF-8 character with the minimum code point in the given data.

EXAMPLE: UTF8::min('abc-äöü-中文空白'); // '-'

  • param string|string[] $arg A UTF-8 encoded string or an array of such strings.
  • psalm-pure
  • return string|null
public static normalize_encoding( $encoding, $fallback = '')
 

Normalize the encoding-"name" input.

EXAMPLE: UTF8::normalize_encoding('UTF8'); // 'UTF-8'

  • param mixed $encoding
  • param mixed $fallback
  • psalm-pure
  • return mixed|string
  • template TNormalizeEncodingFallback
  • phpstan-param string|TNormalizeEncodingFallback $fallback
  • phpstan-return string|TNormalizeEncodingFallback
public static normalize_line_ending(string $str, $replacer = ' ') : string
 

Standardize line ending to unix-like.

  • param string $str
  • param string|string[] $replacer
  • psalm-pure
  • return string
public static normalize_msword(string $str) : string
 

Normalize some MS Word special characters.

EXAMPLE: UTF8::normalize_msword('„Abcdef…”'); // '"Abcdef..."'

  • param string $str
  • psalm-pure
  • return string
public static normalize_whitespace(string $str, bool $keep_non_breaking_space = false, bool $keep_bidi_unicode_controls = false, bool $normalize_control_characters = false) : string
 

Normalize the whitespace.

EXAMPLE: UTF8::normalize_whitespace("abc-\xc2\xa0-öäü-\xe2\x80\xaf-\xE2\x80\xAC", true); // "abc-\xc2\xa0-öäü- -"

  • param string $str
  • param bool $keep_non_breaking_space [optional] Set to true, to keep non-breaking-spaces.
  • param bool $keep_bidi_unicode_controls [optional] Set to true, to keep non-printable (for the web) bidirectional text chars.
  • param bool $normalize_control_characters [optional] Set to true, to convert e.g. LINE-, PARAGRAPH-SEPARATOR with "\n" and LINE TABULATION with "\t".
  • psalm-pure
  • return string
public static ord( $chr, string $encoding = 'UTF-8') : int
 

Calculates Unicode code point of the given UTF-8 encoded character.

INFO: opposite to UTF8::chr()

EXAMPLE: UTF8::ord('☃'); // 0x2603

  • param string $chr
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return int
public static parse_str(string $str, $result, bool $clean_utf8 = false) : bool
 

Parses the string into an array (into the the second parameter).

WARNING: Unlike "parse_str()", this method does not (re-)place variables in the current scope, if the second parameter is not set!

EXAMPLE: UTF8::parse_str('Iñtërnâtiônéàlizætiøn=測試&arr[]=foo+測試&arr[]=ການທົດສອບ', $array); echo $array['Iñtërnâtiônéàlizætiøn']; // '測試'

  • see http://php.net/manual/en/function.parse-str.php
  • param string $str
  • param array<string,mixed> $result
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • psalm-pure
  • return bool <p>Will return <strong>false</strong> if php can't parse the string and we haven't any $result.
public static pcre_utf8_support() : bool
 

Checks if \u modifier is available that enables Unicode support in PCRE.

  • psalm-pure
  • return bool
public static range( $var1, $var2, bool $use_ctype = true, string $encoding = 'UTF-8', $step = 1) : array
 

Create an array containing a range of UTF-8 characters.

EXAMPLE: UTF8::range('κ', 'ζ'); // array('κ', 'ι', 'θ', 'η', 'ζ',)

  • param int|string $var1
  • param int|string $var2
  • param bool $use_ctype
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • param float|int $step [optional] If a step value is given, it will be used as the increment between elements in the sequence. step should be given as a positive number. If not specified, step will default to 1.
  • psalm-pure
  • return list<string>
public static rawurldecode(string $str, bool $multi_decode = true) : string
 

Multi decode HTML entity + fix urlencoded-win1252-chars.

EXAMPLE: UTF8::rawurldecode('tes%20öäü%20ítest+test'); // 'tes öäü ítest+test'

e.g: 'test+test' => 'test+test' 'Düsseldorf' => 'Düsseldorf' 'D%FCsseldorf' => 'Düsseldorf' 'Düsseldorf' => 'Düsseldorf' 'D%26%23xFC%3Bsseldorf' => 'Düsseldorf' 'Düsseldorf' => 'Düsseldorf' 'D%C3%BCsseldorf' => 'Düsseldorf' 'D%C3%83%C2%BCsseldorf' => 'Düsseldorf' 'D%25C3%2583%25C2%25BCsseldorf' => 'Düsseldorf'

  • param string $str
  • param bool $multi_decode
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
private static reduce_string_array(array $strings, bool $remove_empty_values, ?int $remove_short_values = NULL)
 
  • param string[] $strings
  • param bool $remove_empty_values
  • param int|null $remove_short_values
  • psalm-pure
  • return list<string>
public static regex_replace(string $str, string $pattern, string $replacement, string $options = '', string $delimiter = '/') : string
 

Replaces all occurrences of $pattern in $str by $replacement.

  • param string $str
  • param string $pattern
  • param string $replacement
  • param string $options [optional] Matching conditions to be used.
  • param string $delimiter [optional] Delimiter the the regex. Default: '/'
  • psalm-pure
  • return string
public static remove_bom(string $str) : string
 

Remove the BOM from UTF-8 / UTF-16 / UTF-32 strings.

EXAMPLE: UTF8::remove_bom("\xEF\xBB\xBFΜπορώ να"); // 'Μπορώ να'

  • param string $str
  • psalm-pure
  • return string
public static remove_duplicates(string $str, $what = ' ') : string
 

Removes duplicate occurrences of a string in another string.

EXAMPLE: UTF8::remove_duplicates('öäü-κόσμεκόσμε-äöü', 'κόσμε'); // 'öäü-κόσμε-äöü'

  • param string $str
  • param string|string[] $what
  • psalm-pure
  • return string
public static remove_html(string $str, string $allowable_tags = '') : string
 

Remove html via "strip_tags()" from the string.

  • param string $str
  • param string $allowable_tags [optional] You can use the optional second parameter to specify tags which should not be stripped. Default: null
  • psalm-pure
  • return string
public static remove_html_breaks(string $str, string $replacement = '') : string
 

Remove all breaks [ | \r\n | \r | \n | ...] from the string.

  • param string $str
  • param string $replacement [optional] Default is a empty string.
  • psalm-pure
  • return string
public static remove_ileft(string $str, string $substring, string $encoding = 'UTF-8') : string
 

Returns a new string with the prefix $substring removed, if present and case-insensitive.

  • param string $str
  • param string $substring
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string <p>A string without the prefix $substring.
public static remove_invisible_characters(string $str, bool $url_encoded = false, string $replacement = '', bool $keep_basic_control_characters = true) : string
 

Remove invisible characters from a string.

e.g.: This prevents sandwiching null characters between ascii characters, like Java\0script.

EXAMPLE: UTF8::remove_invisible_characters("κόσ\0με"); // 'κόσμε'

copy&past from https://github.com/bcit-ci/CodeIgniter/blob/develop/system/core/Common.php

  • param string $str
  • param bool $url_encoded [optional] Try to remove url encoded control character. WARNING: maybe contains false-positives e.g. aa%0Baa -> aaaa. Default: false
  • param string $replacement [optional] The replacement character.
  • param bool $keep_basic_control_characters [optional] Keep control characters like [LRM] or [LSEP].
  • psalm-pure
  • return string
public static remove_iright(string $str, string $substring, string $encoding = 'UTF-8') : string
 

Returns a new string with the suffix $substring removed, if present and case-insensitive.

  • param string $str
  • param string $substring
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string <p>A string having a $str without the suffix $substring.
public static remove_left(string $str, string $substring, string $encoding = 'UTF-8') : string
 

Returns a new string with the prefix $substring removed, if present.

  • param string $str
  • param string $substring
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string <p>A string without the prefix $substring.
public static remove_right(string $str, string $substring, string $encoding = 'UTF-8') : string
 

Returns a new string with the suffix $substring removed, if present.

  • param string $str
  • param string $substring
  • param string $encoding [optional] Default: 'UTF-8'
  • psalm-pure
  • return string <p>A string having a $str without the suffix $substring.
public static replace(string $str, string $search, string $replacement, bool $case_sensitive = true) : string
 

Replaces all occurrences of $search in $str by $replacement.

  • param string $str
  • param string $search
  • param string $replacement
  • param bool $case_sensitive [optional] Whether or not to enforce case-sensitivity. Default: true
  • psalm-pure
  • return string
public static replace_all(string $str, array $search, $replacement, bool $case_sensitive = true) : string
 

Replaces all occurrences of $search in $str by $replacement.

  • param string $str
  • param string[] $search
  • param string|string[] $replacement
  • param bool $case_sensitive [optional] Whether or not to enforce case-sensitivity. Default: true
  • psalm-pure
  • return string
public static replace_diamond_question_mark(string $str, string $replacement_char = '', bool $process_invalid_utf8_chars = true) : string
 

Replace the diamond question mark (�) and invalid-UTF8 chars with the replacement.

EXAMPLE: UTF8::replace_diamond_question_mark('中文空白�', ''); // '中文空白'

  • param string $str
  • param string $replacement_char
  • param bool $process_invalid_utf8_chars
  • psalm-pure
  • return string
public static rtrim(string $str = '', ?string $chars = NULL) : string
 

Strip whitespace or other characters from the end of a UTF-8 string.

EXAMPLE: UTF8::rtrim('-ABC-中文空白- '); // '-ABC-中文空白-'

  • param string $str
  • param string|null $chars
  • psalm-pure
  • return string
private static rxClass(string $s, string $class = '')
 

rxClass

  • param string $s
  • param string $class
  • return string
  • psalm-pure
public static showSupport(bool $useEcho = true)
 

WARNING: Print native UTF-8 support (libs) by default, e.g. for debugging.

  • param bool $useEcho
  • psalm-pure
  • return string|void
  • phpstan-return ($useEcho is true ? void : string)
public static single_chr_html_encode(string $char, bool $keep_ascii_chars = false, string $encoding = 'UTF-8') : string
 

Converts a UTF-8 character to HTML Numbered Entity like "{".

EXAMPLE: UTF8::single_chr_html_encode('κ'); // 'κ'

  • param string $char
  • param bool $keep_ascii_chars
  • param string $encoding [optional] Set the charset for e.g. "mb_" function
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $char
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static spaces_to_tabs(string $str, int $tab_length = 4) : string
 
  • param string $str
  • param int<1, max> $tab_length
  • psalm-pure
  • return string
  • template T as string
  • phpstan-param T $str
  • phpstan-return (T is non-empty-string ? non-empty-string : string)
public static str_camelize(string $str, string $encoding = 'UTF-8', bool $clean_utf8 = false, ?string $lang = NULL, bool $try_to_keep_the_string_length = false) : string
 

Returns a camelCase version of the string. Trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, as well as underscores.

  • param string $str
  • param string $encoding [optional] Default: 'UTF-8'
  • param bool $clean_utf8 [optional] Remove non UTF-8 chars from the string.
  • param string|null $lang [optional] Set the language for special cases: az, el, lt, tr
  • param bool $try_to_keep_the_string_length [optional] true === try to keep the string length: e.g. ẞ -> ß
  • psalm-pure
  • return string
public static str_capitalize_name(string $str) : string
 

Returns the string with the first letter of each word capitalized, except for when the word is a name which shouldn't be capitalized.

  • param string $str
  • psalm-pure
  • return string <p>A string with $str capitalized.
private static str_capitalize_name_helper(string $names, string $delimiter, string $encoding = 'UTF-8')
 

Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius").

  • param string $names
  • param string $delimiter
  • param string $encoding
  • phpstan-param non-empty-string $delimiter
  • psalm-pure
  • return string
public static str_contains(string $haystack, string $needle, bool $case_sensitive