Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ public static function seedTestUnicodeToUtf8(): array
return [
["\u0422\u0435\u0441\u0442 \u0441\u0438\u0441\u0442\u0435\u043c\u044b", "Тест системы"],
["\u00dcberpr\u00fcfung der Systemumstellung", "Überprüfung der Systemumstellung"],
'surrogate pair' => ['\uD83D\uDE00 smile', '😀 smile'],
'surrogate pair in lowercase' => ['\ud83d\ude00', '😀'],
'surrogate pair next to a BMP character' => ['\u00e9\uD83D\uDE00\u00e9', 'é😀é'],
];
}

Expand All @@ -470,6 +473,9 @@ public static function seedTestUnicodeToUtf16(): array
return [
["\u0422\u0435\u0441\u0442 \u0441\u0438\u0441\u0442\u0435\u043c\u044b", "Тест системы"],
["\u00dcberpr\u00fcfung der Systemumstellung", "Überprüfung der Systemumstellung"],
'surrogate pair' => ['\uD83D\uDE00 smile', '😀 smile'],
'surrogate pair in lowercase' => ['\ud83d\ude00', '😀'],
'surrogate pair next to a BMP character' => ['\u00e9\uD83D\uDE00\u00e9', 'é😀é'],
];
}

Expand Down
14 changes: 14 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# String package

TODO

## `StringHelper::unicode_to_utf8()` and `unicode_to_utf16()`

Both methods replace `\uXXXX` escape sequences, as they appear in JSON, with the characters they
stand for. A surrogate pair such as `\uD83D\uDE00` is decoded into one character (U+1F600). Despite
its name, `unicode_to_utf16()` also returns UTF-8. The two methods differ only in how they handle a
lone surrogate (one without its partner): `unicode_to_utf8()` encodes it as three bytes that are not
valid UTF-8, and `unicode_to_utf16()` turns it into `?`.

```php
use Joomla\String\StringHelper;

StringHelper::unicode_to_utf8('caf\u00e9 \uD83D\uDE00'); // 'café 😀'
```
50 changes: 27 additions & 23 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
*/
public static function str_pad($input, $length, $padStr = ' ', $type = STR_PAD_RIGHT)
{
return mb_str_pad($input, $length, $padStr, $type);

Check failure on line 333 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_str_pad not found.
}

/**
Expand Down Expand Up @@ -605,10 +605,10 @@
}

if ($charlist === false) {
return mb_ltrim($str);

Check failure on line 608 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_ltrim not found.
}

return mb_ltrim($str, $charlist);

Check failure on line 611 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_ltrim not found.
}

/**
Expand All @@ -632,10 +632,10 @@
}

if ($charlist === false) {
return mb_rtrim($str);

Check failure on line 635 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_rtrim not found.
}

return mb_rtrim($str, $charlist);

Check failure on line 638 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_rtrim not found.
}

/**
Expand All @@ -659,10 +659,10 @@
}

if ($charlist === false) {
return mb_trim($str);

Check failure on line 662 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_trim not found.
}

return mb_trim($str, $charlist);

Check failure on line 665 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_trim not found.
}

/**
Expand All @@ -684,14 +684,14 @@
public static function ucfirst($str, $delimiter = null, $newDelimiter = null)
{
if ($delimiter === null) {
return mb_ucfirst($str);

Check failure on line 687 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_ucfirst not found.
}

if ($newDelimiter === null) {
$newDelimiter = $delimiter;
}

return implode($newDelimiter, array_map('mb_ucfirst', explode($delimiter, $str)));

Check failure on line 694 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Parameter #1 $callback of function array_map expects (callable(string): mixed)|null, 'mb_ucfirst' given.
}

/**
Expand Down Expand Up @@ -906,7 +906,9 @@
}

/**
* Converts Unicode sequences to UTF-8 string.
* Converts Unicode escape sequences (\uXXXX) to a UTF-8 string.
*
* A surrogate pair such as "\uD83D\uDE00" is decoded into a single character.
*
* @param string $str Unicode string to convert
*
Expand All @@ -916,40 +918,42 @@
*/
public static function unicode_to_utf8($str)
{
if (\extension_loaded('mbstring')) {
return preg_replace_callback(
'/\\\\u([0-9a-fA-F]{4})/',
static function ($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
},
$str
);
}
return preg_replace_callback(
'/\\\\u(D[89AB][0-9A-F]{2})\\\\u(D[C-F][0-9A-F]{2})|\\\\u([0-9A-F]{4})/i',
static function ($match) {
if ($match[1] !== '') {
return mb_convert_encoding(pack('H*', $match[1] . $match[2]), 'UTF-8', 'UTF-16BE');
}

return $str;
return mb_convert_encoding(pack('H*', $match[3]), 'UTF-8', 'UCS-2BE');
},
$str
);
}

/**
* Converts Unicode sequences to UTF-16 string.
* Converts UTF-16 escape sequences (\uXXXX), as used in JSON, to a UTF-8 string.
*
* A surrogate pair such as "\uD83D\uDE00" is decoded into a single character.
*
* @param string $str Unicode string to convert
*
* @return string UTF-16 string
* @return string UTF-8 string. Despite the method name, the result is UTF-8, not UTF-16.
*
* @since 1.3.0
*/
public static function unicode_to_utf16($str)
{
if (\extension_loaded('mbstring')) {
return preg_replace_callback(
'/\\\\u([0-9a-fA-F]{4})/',
static function ($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UTF-16BE');
},
$str
);
}
return preg_replace_callback(
'/\\\\u(D[89AB][0-9A-F]{2})\\\\u(D[C-F][0-9A-F]{2})|\\\\u([0-9A-F]{4})/i',
static function ($match) {
if ($match[1] !== '') {
return mb_convert_encoding(pack('H*', $match[1] . $match[2]), 'UTF-8', 'UTF-16BE');
}

return $str;
return mb_convert_encoding(pack('H*', $match[3]), 'UTF-8', 'UTF-16BE');
},
$str
);
}
}
Loading