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
7 changes: 7 additions & 0 deletions Tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ public static function seedTestStr_ireplace(): array
[['PIG', 'JUMPED'], ['cow', 'hopped'], 'the pig jumped over the pig', true, 'the cow hopped over the cow'],
['шил', 'биш', 'Би шил идэй чадна', true, 'Би биш идэй чадна'],
['/', ':', '/test/slashes/', true, ':test:slashes:'],
'lowercase is longer than the original' => ['x', 'y', 'Ⱥx', null, 'Ⱥy'],
'lowercase is shorter than the original' => ['ab', 'Z', 'ẞAB', null, 'ẞZ'],
'case variant of different byte length' => ['ß', 'ss', 'STRAẞE', null, 'STRAssE'],
'regular expression characters in search' => ['.*', '!', 'a.*b.*', null, 'a!b!'],
'replacement is taken literally' => ['b', '$0', 'abc', null, 'a$0c'],
'count limits the replacements' => ['b', 'c', 'abab', 1, 'acab'],
'count of zero replaces nothing' => ['b', 'c', 'abab', 0, 'abab'],
];
}

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

TODO

## `StringHelper::str_ireplace()`

`StringHelper::str_ireplace($search, $replace, $str, $count = null)` replaces every case-insensitive
occurrence of `$search` in `$str`. It matches on the original string with Unicode case folding, so
characters whose lowercase form has a different byte length (`Ⱥ`, `ẞ`) are handled correctly, and the
replacement is inserted literally.

Unlike PHP's `str_ireplace()`, the fourth argument is **not** a by-reference variable that receives
the number of replacements. It is a limit: at most `$count` occurrences of each search string are
replaced. `null`, `true` and `false` mean no limit.

```php
use Joomla\String\StringHelper;

StringHelper::str_ireplace('b', 'c', 'abAB'); // 'acAc'
StringHelper::str_ireplace('b', 'c', 'abAB', 1); // 'acAB'
```
36 changes: 16 additions & 20 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@
* @param string|string[] $search String to search
* @param string|string[] $replace Existing string to replace
* @param string $str New string to replace with
* @param integer|null|boolean $count Optional count value to be passed by reference
* @param integer|null|boolean $count Optional maximum number of replacements per search string. Unlike the native
* str_ireplace(), this is a limit, not a by-reference variable that receives
* the number of replacements. Null, true or false mean no limit.
*
* @return string UTF-8 String
*
Expand All @@ -272,29 +274,23 @@
public static function str_ireplace($search, $replace, $str, $count = null)
{
if (!is_array($search)) {
$slen = strlen($search);
if ($slen == 0) {
$search = (string) $search;

if ($search === '') {
return $str;
}

$lendif = strlen($replace) - strlen($search);
$search = mb_strtolower($search);
// Match on the original string, so offsets and lengths are not affected by case mapping
$result = preg_replace_callback(
'/' . preg_quote($search, '/') . '/iu',
static function () use ($replace) {
return (string) $replace;
},
$str,
\is_int($count) ? $count : -1
);

$search = preg_quote($search, '/');
$lstr = mb_strtolower($str);
$i = 0;
$matched = 0;
while (preg_match('/(.*)' . $search . '/Us', $lstr, $matches)) {
if ($i === $count) {
break;
}
$mlen = strlen($matches[0]);
$lstr = substr($lstr, $mlen);
$str = substr_replace($str, $replace, $matched + strlen($matches[1]), $slen);
$matched += $mlen + $lendif;
$i++;
}
return $str;
return $result ?? $str;
} else {
foreach (array_keys($search) as $k) {
if (is_array($replace)) {
Expand Down Expand Up @@ -330,7 +326,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 329 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 +601,10 @@
}

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

Check failure on line 604 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 607 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 +628,10 @@
}

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

Check failure on line 631 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 634 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 +655,10 @@
}

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

Check failure on line 658 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 661 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 +680,14 @@
public static function ucfirst($str, $delimiter = null, $newDelimiter = null)
{
if ($delimiter === null) {
return mb_ucfirst($str);

Check failure on line 683 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 690 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
Loading