I'm working a way to search for specific words in a text and highlight them. The code works perfectly, except I would like that it also matches similar letters.I mean, searching for fête should match fêté, fete, ...
Is there an easy & elegant way to do this?
This is my current code:
$regex='/(' . preg_replace('/\s+/', '|', preg_quote($usersearchstring)) .')/iu';$higlightedtext = preg_replace($regex, '<span class="marked-search-text">\0</span>', $text);
My text is not html encoded. And searching in MariaDB matches the similar results.
[edit]And here a longer example of the issue:
$usersearchstring='fête';$text='la paix fêtée avec plus de 40 cultures';$regex='/(' . preg_replace('/\s+/', '|', preg_quote($usersearchstring)) .')/iu';$higlightedtext = preg_replace($regex, '<span class="marked-search-text">\0</span>', $text);
Result is that $higlightedtext is identical to $text
When changing $higlightedtext the word "fêté" then $higlightedtext is
'la paix <span class="marked-search-text">fêté</span>e avec plus de 40 cultures'
However, I want it to match "always" all the variations of letters, since there can be (and are in reality) many variations of the words possible.And we have fête fêté and possible even fete in the database.
And I have been thinking about this, but the only solution I see is to have an huge array with all letter replacement options, then loop over them and try every variation. But that is not elegant and will be slow.(Since for many letters I have at least 5 variations: aáàâä, resulting in, if the word has 3 vowels that I need to do 75x (5x5x5) the preg_replace.
[/edit]