Remove flaky iconv and replace with PHP-intl

This commit is contained in:
Alex Cabal 2019-01-18 20:51:22 -06:00
parent ba53958596
commit b812485d8a
3 changed files with 15 additions and 9 deletions

View file

@ -1,12 +1,19 @@
<?
class Formatter{
public static function MakeUrlSafe(string $text): string{
// Remove apostrophes
// We have to do this first so iconv doesn't choke
$text = str_replace(['\'', ',', ''], '', $text);
public static function RemoveDiacritics(string $text): string{
if(!isset($GLOBALS['transliterator'])){
$GLOBALS['transliterator'] = Transliterator::createFromRules(':: Any-Latin; :: Latin-ASCII; :: NFD; :: [:Nonspacing Mark:] Remove; :: Lower(); :: NFC;', Transliterator::FORWARD);
}
return $GLOBALS['transliterator']->transliterate($text);
}
public static function MakeUrlSafe(string $text): string{
// Remove accent characters
$text = iconv('UTF-8', 'ASCII//TRANSLIT', $text) ?: '';
$text = self::RemoveDiacritics($text);
// Remove apostrophes
$text = str_replace('\'', '', $text);
// Trim and convert to lowercase
$text = mb_strtolower(trim($text));