Skip to content

Commit

Permalink
bug #38195 [String] improve slugger's portability accross implementat…
Browse files Browse the repository at this point in the history
…ions of iconv() (nicolas-grekas)

This PR was merged into the 5.1 branch.

Discussion
----------

[String] improve slugger's portability accross implementations of iconv()

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36880, fix #38060
| License       | MIT
| Doc PR        | -

Ping @Surfoo @edguar, can you please confirm that this fixes your issues?

Commits
-------

21e4e32 [String] improve slugger's portability accross implementations of iconv()
  • Loading branch information
fabpot committed Sep 16, 2020
2 parents ae655bf + 21e4e32 commit f27ca4e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Symfony/Component/String/AbstractUnicodeString.php
Expand Up @@ -137,15 +137,15 @@ public function ascii(array $rules = []): self
}
} elseif (!\function_exists('iconv')) {
$s = preg_replace('/[^\x00-\x7F]/u', '?', $s);
} elseif (\ICONV_IMPL === 'glibc') {
$s = iconv('UTF-8', 'ASCII//TRANSLIT', $s);
} else {
$s = @preg_replace_callback('/[^\x00-\x7F]/u', static function ($c) {
if ('' === $c = (string) iconv('UTF-8', 'ASCII//IGNORE//TRANSLIT', $c[0])) {
$c = (string) iconv('UTF-8', 'ASCII//TRANSLIT', $c[0]);

if ('' === $c && '' === iconv('UTF-8', 'ASCII//TRANSLIT', '²')) {
throw new \LogicException(sprintf('"%s" requires a translit-able iconv implementation, try installing "gnu-libiconv" if you\'re using Alpine Linux.', static::class));
}

return 1 < \strlen($c) ? ltrim($c, '\'`"^~') : (\strlen($c) ? $c : '?');
return 1 < \strlen($c) ? ltrim($c, '\'`"^~') : ('' !== $c ? $c : '?');
}, $s);
}
}
Expand Down

0 comments on commit f27ca4e

Please sign in to comment.