: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Converts the decimal value of a multibyte character string
* @param array $array Array
public static function ascii(array $array)
foreach ($array as $utf8) {
} elseif ($utf8 < 2048) {
$ascii .= chr(192 + (($utf8 - ($utf8 % 64)) / 64));
$ascii .= chr(128 + ($utf8 % 64));
$ascii .= chr(224 + (($utf8 - ($utf8 % 4096)) / 4096));
$ascii .= chr(128 + ((($utf8 % 4096) - ($utf8 % 64)) / 64));
$ascii .= chr(128 + ($utf8 % 64));
* Converts filesize from human readable string to bytes
* @param string $size Size in human readable string like '5MB', '5M', '500B', '50kb' etc.
* @param mixed $default Value to be returned when invalid size was used, for example 'Unknown type'
* @return mixed Number of bytes as integer on success, `$default` on failure if not false
* @throws \InvalidArgumentException On invalid Unit type.
* @link https://book.cakephp.org/3/en/core-libraries/text.html#Cake\Utility\Text::parseFileSize
public static function parseFileSize($size, $default = false)
if (ctype_digit($size)) {
$size = strtoupper($size);
$i = array_search(substr($size, -2), ['KB', 'MB', 'GB', 'TB', 'PB'], true);
$i = array_search(substr($size, -1), ['K', 'M', 'G', 'T', 'P'], true);
$size = (float)substr($size, 0, $l);
return (int)($size * pow(1024, $i + 1));
if (substr($size, -1) === 'B' && ctype_digit(substr($size, 0, -1))) {
$size = substr($size, 0, -1);
if ($default !== false) {
throw new InvalidArgumentException('No unit type.');
* Get the default transliterator.
* @return \Transliterator|null Either a Transliterator instance, or `null`
* in case no transliterator has been set yet.
public static function getTransliterator()
return static::$_defaultTransliterator;
* Set the default transliterator.
* @param \Transliterator $transliterator A `Transliterator` instance.
public static function setTransliterator(\Transliterator $transliterator)
static::$_defaultTransliterator = $transliterator;
* Get default transliterator identifier string.
* @return string Transliterator identifier.
public static function getTransliteratorId()
return static::$_defaultTransliteratorId;
* Set default transliterator identifier string.
* @param string $transliteratorId Transliterator identifier.
public static function setTransliteratorId($transliteratorId)
$transliterator = transliterator_create($transliteratorId);
if ($transliterator === null) {
throw new Exception('Unable to create transliterator for id: ' . $transliteratorId);
static::setTransliterator($transliterator);
static::$_defaultTransliteratorId = $transliteratorId;
* @param string $string String to transliterate.
* @param \Transliterator|string|null $transliterator Either a Transliterator
* instance, or a transliterator identifier string. If `null`, the default
* transliterator (identifier) set via `setTransliteratorId()` or
* `setTransliterator()` will be used.
* @see https://secure.php.net/manual/en/transliterator.transliterate.php
public static function transliterate($string, $transliterator = null)
$transliterator = static::$_defaultTransliterator ?: static::$_defaultTransliteratorId;
$return = transliterator_transliterate($transliterator, $string);
throw new Exception(sprintf('Unable to transliterate string: %s', $string));
* Returns a string with all spaces converted to dashes (by default),
* characters transliterated to ASCII characters, and non word characters removed.
* - `replacement`: Replacement string. Default '-'.
* - `transliteratorId`: A valid transliterator id string.
* If `null` (default) the transliterator (identifier) set via
* `setTransliteratorId()` or `setTransliterator()` will be used.
* If `false` no transliteration will be done, only non words will be removed.
* - `preserve`: Specific non-word character to preserve. Default `null`.
* For e.g. this option can be set to '.' to generate clean file names.
* @param string $string the string you want to slug
* @param array|string $options If string it will be use as replacement character
* or an array of options.
* @see setTransliterator()
* @see setTransliteratorId()
public static function slug($string, $options = [])
if (is_string($options)) {
$options = ['replacement' => $options];
'transliteratorId' => null,
if ($options['transliteratorId'] !== false) {
$string = static::transliterate($string, $options['transliteratorId']);
$regex = '^\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}';
if ($options['preserve']) {
$regex .= preg_quote($options['preserve'], '/');
$quotedReplacement = preg_quote((string)$options['replacement'], '/');
'/[' . $regex . ']/mu' => $options['replacement'],
sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
if (is_string($options['replacement']) && strlen($options['replacement']) > 0) {
$map[sprintf('/[%s]+/mu', $quotedReplacement)] = $options['replacement'];
$string = preg_replace(array_keys($map), $map, $string);