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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"phpstan/phpstan-deprecation-rules": "^2.0.3"
},
"suggest": {
"ext-iconv": "To transcode strings with transliteration in StringHelper::transcode()",
"ext-mbstring": "For improved processing",
"doctrine/inflector": "To use the string inflector"
},
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::transcode()`

`StringHelper::transcode($source, $fromEncoding, $toEncoding)` converts a string between character
sets. With `ext-iconv` it uses `iconv()` with transliteration, so characters the target encoding
cannot represent are approximated or dropped (`€` becomes `EUR` in ISO-8859-1). `ext-iconv` is
suggested, not required. Without it, `transcode()` uses `mb_convert_encoding()`, which replaces
such characters with `?`.

```php
use Joomla\String\StringHelper;

StringHelper::transcode('Åbc €100', 'UTF-8', 'ISO-8859-1'); // "\xC5bc EUR100" with ext-iconv, "\xC5bc ?100" without
```
7 changes: 7 additions & 0 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 @@ -724,6 +724,9 @@
/**
* Transcode a string.
*
* Uses iconv() with transliteration when the iconv extension is available. Without it, the string is converted with
* mb_convert_encoding(), which replaces characters that the target encoding cannot represent with "?".
*
* @param string $source The string to transcode.
* @param string $fromEncoding The source encoding.
* @param string $toEncoding The target encoding.
Expand All @@ -736,6 +739,10 @@
*/
public static function transcode($source, $fromEncoding, $toEncoding)
{
if (!\defined('ICONV_IMPL')) {
return mb_convert_encoding($source, $toEncoding, $fromEncoding);
}

switch (ICONV_IMPL) {
case 'glibc':
return @iconv($fromEncoding, $toEncoding . '//TRANSLIT,IGNORE', $source);
Expand Down
Loading