How to compare two strings case and diacritic-insensitive?

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

I have two strings

String 1: "sebastien"

String 2: "Sébastien"

I want to compare these two strings by ignoring é (Accents) character. Can anyone know this logic?

Thanks in advance

回答1:



回答2:

Calculate the similarity between two strings with similar_text function.

Before parse "é" with :

See more at: http://bitprison.net/php_normalize_string_function#sthash.tMoMbaOG.dpuf



回答3:

Use Normalizer PHP extension or iconv, more info:

http://www.php.net/manual/en/class.normalizer.php http://www.php.net/manual/en/function.iconv.php  <?php $string = 'Sébastien'; echo Normalizer::normalize($string); echo iconv('UTF-8', 'ASCII//TRANSLIT', $string); ?> 


回答4:

You can also use behat/transliterator package:

require_once 'vendor/autoload.php';  $stringWithoutAccent = 'acces'; $stringWithAccent = 'accès';  // true var_dump($stringWithoutAccent === \Behat\Transliterator\Transliterator::unaccent($stringWithAccent)); 


回答5:

You can simply decode the strings in utf8 and compare them.

Otherwise, if by ignore, you meant not compare the character at all, then you might have to do a char-by-char comparison. First, get both strings to upper or lower case, and wherever you come across a non unicode character, skip the comparison and move to comparing the next char.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!