本文介绍了PHP大小写和变音不敏感搜索到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含单词的数组,其中有些带有重音。我想测试给定的单词是否进入该数组,但使其不区分大小写和重音。例如:

I have an array containing words, some of them with accents. I want to test if a given word is into that array but making it case and accent insensitive. For example:

$array = array("coche","camión","moto","carro");

我想要一个简单的小功能,例如 in_array 。如果我的字符串是'Camion''camión',则应返回 true

i want a simple little function, something like in_array. If my string is 'Camion' or 'camión' it should return true.

有什么想法吗?

推荐答案

尝试一下输出::-D

Try this out: :-D

function check_array($array, $string){
   $trans = array("é" => "e", "é" => "e", "á" => "a", "á" => "a", "í" => "i","í"=>"i", "ó"=>"o", "ó" => "o", "ú" => "u", "ú"=>"u","ö" => "u", "ü"=>"u");
   $realString = strtr($string,$trans);
   foreach($array as $val){
      $realVal = strtr($val,$trans);
      if(strcasecmp( $realVal, $realString ) == 0){
         return true;
      }
   }
   return false;
}

要使用它:

check_array($array, 'Camion');

使用 strcasecmp 按照建议

这篇关于PHP大小写和变音不敏感搜索到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:04