I've been searching everywhere in order to find a way to transliterate a search term before initiating the search in a different language - greek in particular. So I would expect a term written in greeklish (greek in english chracters) to be searched as:
I was thinking about taking the search term and use str_replace
to replace characters, but there might be different variations of the same character. Any ideas on how to tackle this?
I've been searching everywhere in order to find a way to transliterate a search term before initiating the search in a different language - greek in particular. So I would expect a term written in greeklish (greek in english chracters) to be searched as:
I was thinking about taking the search term and use str_replace
to replace characters, but there might be different variations of the same character. Any ideas on how to tackle this?
If you have PHP's Intl extension installed, you can use the wonderful Transliterator class.
I get some pretty good results, like this:
$t = Transliterator::create('Latin-Greek', Transliterator::FORWARD );
var_dump( [
'alfa' => $t->transliterate('alfa'),
'alpha' => $t->transliterate('alpha'),
] );
Both these greeklish forms give me "άλφα"
. I don't know how it knows where to put the accent, but it's pretty impressive.
Once you have the greek script, the remaining nuances should be taken care of by the database collation. "α" will match "ά" and "σ" will even match "ς" (This works on my system running MySQL with collation utf8mb4_unicode_520_ci)
If you don't have this extension, or the underlying ICU library isn't available, then you'd be looking at expanding the characters yourself, but transliteration is hard so I don't recommend it.