dousui3124 2014-10-22 18:00
浏览 113
已采纳

有没有办法比较西班牙语中的两个字符串,无论PHP中的重音如何?

My question is, given i have the following php code to compare two strings:

   $cadena1='JUAN LÓPEZ YÁÑEZ';
   $cadena2='JUAN LOPEZ YÁÑEZ';

   if($cadena1===$cadena2){
     echo '<p style="color: green;">The strings match!</p>';
   }else{
     echo '<p style="color: red;">The strings do not match. Accent sensitive?</p>';
   }

I notice for example that if I compare LOPEZ and LÓPEZ then the comparison turns to false.

Is there a way or a function already there to compare theses strings regardless of the Spanish accents?

  • 写回答

5条回答 默认 最新

  • doudao1837 2014-10-22 18:06
    关注

    I would replace all accents in your strings before comparing them. You can do that using the following code:

    $replacements = array('Ó'=>'O', 'Á'=>'A', 'Ñ' => 'N'); //Add the remaining Spanish accents. 
    $output = strtr("JUAN LÓPEZ YÁÑEZ",$replacements);
    

    output will now be equal to cadena2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?