最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

translation - Transliterating search term in another language

matteradmin7PV0评论

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:

  • the search term 'as is'
  • or search term transliterated in greek characters

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:

  • the search term 'as is'
  • or search term transliterated in greek characters

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?

Share Improve this question asked Feb 26, 2019 at 15:13 scooterlordscooterlord 1137 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

Post a comment

comment list (0)

  1. No comments so far