From b812485d8ae4a2920a73514df5ce1de8d96fe999 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Fri, 18 Jan 2019 20:51:22 -0600 Subject: [PATCH] Remove flaky iconv and replace with PHP-intl --- README.md | 2 +- lib/Ebook.php | 5 ++--- lib/Formatter.php | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 573d9e39..fd1bd0f4 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ You’ll 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` diff --git a/lib/Ebook.php b/lib/Ebook.php index 621d523d..ed15ca4e 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -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; diff --git a/lib/Formatter.php b/lib/Formatter.php index 870a37a5..6c51c417 100644 --- a/lib/Formatter.php +++ b/lib/Formatter.php @@ -1,12 +1,19 @@ 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));