s of values, space around values are trimmed. */ public static function uniqueList(string $list): string { return implode(',', array_unique(GeneralUtility::trimExplode(',', $list, true))); } /** * Works the same as str_pad() except that it correctly handles strings with multibyte characters * and takes an additional optional argument $encoding. */ public static function multibyteStringPad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, string $encoding = 'UTF-8'): string { $len = mb_strlen($string, $encoding); $pad_string_len = mb_strlen($pad_string, $encoding); if ($len >= $length || $pad_string_len === 0) { return $string; } switch ($pad_type) { case STR_PAD_RIGHT: $string .= str_repeat($pad_string, (int)(($length - $len) / $pad_string_len)); $string .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len); return $string; case STR_PAD_LEFT: $leftPad = str_repeat($pad_string, (int)(($length - $len) / $pad_string_len)); $leftPad .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len); return $leftPad . $string; case STR_PAD_BOTH: $leftPadCount = (int)(($length - $len) / 2); $len += $leftPadCount; $padded = ((int)($leftPadCount / $pad_string_len)) * $pad_string_len; $leftPad = str_repeat($pad_string, (int)($leftPadCount / $pad_string_len)); $leftPad .= mb_substr($pad_string, 0, $leftPadCount - $padded); $string = $leftPad . $string . str_repeat($pad_string, (int)(($length - $len) / $pad_string_len)); $string .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len); return $string; } return $string; } /** * Returns base64 encoded value with a URL and filename safe alphabet * according to https://tools.ietf.org/html/rfc4648#section-5 * * The difference to classic base64 is, that the result * alphabet is adjusted like shown below, padding (`=`) * is stripped completely: * + position #62: `+` -> `-` (minus) * + position #63: `/` -> `_` (underscore) * * @param string $value raw value * @return string base64url encoded string */ public static function base64urlEncode(string $value): string { return strtr(base64_encode($value), ['+' => '-', '/' => '_', '=' => '']); } /** * Returns base64 decoded value with a URL and filename safe alphabet * according to https://tools.ietf.org/html/rfc4648#section-5 * * The difference to classic base64 is, that the result * alphabet is adjusted like shown below, padding (`=`) * is stripped completely: * + position #62: `-` (minus) -> `+` * + position #63: `_` (underscore) -> `/` * * @param string $value base64url decoded string * @param bool $strict enforces to only allow characters contained in the base64(url) alphabet * @return string|false raw value, or `false` if non-base64(url) characters were given in strict mode */ public static function base64urlDecode(string $value, bool $strict = false): string|false { return base64_decode(strtr($value, ['-' => '+', '_' => '/']), $strict); } /** * Explodes a string while respecting escape characters * * e.g.: delimiter: '.'; escapeCharacter: '\'; subject: 'new\.site.child' * result: [new.site, child] * @param string $delimiter * @param string $subject * @param string $escapeCharacter */ public static function explodeEscaped(string $delimiter, string $subject, string $escapeCharacter = '\\'): array { if ($delimiter !== '') { $placeholder = '\\0\\0\\0_esc'; $subjectEscaped = str_replace($escapeCharacter . $delimiter, $placeholder, $subject); $escapeParts = explode($delimiter, $subjectEscaped); foreach ($escapeParts as &$part) { $part = str_replace($placeholder, $delimiter, $part); } return $escapeParts; } return [$subject]; } }