From 8a8692619c5eb3024b1f5cdd58bc6aebf5b8df64 Mon Sep 17 00:00:00 2001 From: Mike Colagrosso Date: Sun, 21 Jan 2024 14:41:23 -0700 Subject: [PATCH] Keep words with apostrophes intact when searching The code will convert a search for: `Whistler's Mother` into: `\b(whistler's|mother)\b` Before this fix, it was: `\b(whistler|s|mother)\b` --- lib/Library.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/Library.php b/lib/Library.php index c1cf7d82..bc75c5df 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -213,10 +213,14 @@ class Library{ $orderBy = 'art.CompletedYear desc'; } - // Remove diacritics and non-alphanumeric characters - $query = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', Formatter::RemoveDiacritics($query ?? ''))); + // Remove diacritics and non-alphanumeric characters, but preserve apostrophes + $query = trim(preg_replace('|[^a-zA-Z0-9\' ]|ius', ' ', Formatter::RemoveDiacritics($query ?? ''))); - $tokenizedQuery = '\b(' . implode('|', preg_split('/\b|\s+/', $query, -1, PREG_SPLIT_NO_EMPTY)) . ')\b'; + // Split the query on word boundaries followed by spaces. This keeps words with apostrophes intact. + $tokenArray = preg_split('/\b\s+/', $query, -1, PREG_SPLIT_NO_EMPTY); + + // Join the tokens with '|' to search on any token, but add word boundaries to force the full token to match + $tokenizedQuery = '\b(' . implode('|', $tokenArray) . ')\b'; $params[] = $tokenizedQuery; // art.Name $params[] = $tokenizedQuery; // art.EbookWwwFilesystemPath