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

@ -12,7 +12,7 @@ Youll also need to ensure the following:
- Your PHP installation must be configured to have `/standardebooks.org/lib/` in its include path.
- PHP-APCu must be installed. On Ubuntu this can be done with `sudo apt install php-apcu`.
- [PHP-APCu](http://php.net/manual/en/book.apcu.php) and [PHP-intl](http://php.net/manual/en/book.intl.php) must be installed. On Ubuntu this can be done with `sudo apt install php-apcu php-intl`.
- The URL `^/ebooks/([^\.]+?)/?$` must redirect to `/standardebooks.org/ebooks/ebook.php?url-path=$1`

View file

@ -342,9 +342,8 @@ class Ebook{
}
// Remove diacritics and non-alphanumeric characters
$searchString = str_replace([',', ''], '', $searchString);
$searchString = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', iconv('UTF-8', 'ASCII//TRANSLIT', $searchString) ?: '') ?? '');
$query = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', iconv('UTF-8', 'ASCII//TRANSLIT', $query) ?: '') ?? '');
$searchString = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', Formatter::RemoveDiacritics($searchString)) ?? '');
$query = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', Formatter::RemoveDiacritics($query)) ?? '');
if($query == ''){
return false;

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));