比较两个字符串,显然它们是相等的


Compare two string apparently they are equal

我正在尝试比较两个字符串。我从数组中获得第一个字符串,第二个字符串用于读取目录。问题是,我试图比较数组名称与正在读取的文件的名称是否相同。字符串为:

algo está cambiando.mp3

它存在于数组和目录中。

我把以下信息打印出来进行检查:

阵列长度:30 algo estácambiando.mp3//阵列元素

文件长度:23 algo estácambiando.mp3//文件

我用这个代码做比较,总是抛出错误。我不知道,因为他们不一样。

echo  "<br>array length:  ".mb_strlen(($arrayPosiciones[$x])). " ".  ($arrayPosiciones[$x]);
echo  "<br>file length:".mb_strlen(($d)). "  ".($d)  ;

我想这是口音的问题。我尝试了很多方法。甚至是我为有波浪号的项目放置的数组。使用htmlentities,单词显示正确。

for($i=0; $i<count($arrayPosiciones); $i++){
  $arrayPosiciones[$i]=htmlentities($arrayPosiciones[$i]);
}

我能做什么?我正在迭代一个目录,并比较当前文件是否等于$arrayPosiciones中的值。

$ruta=$_REQUEST['ruta'];
$dir =scandir($ruta);
foreach($dir as $d){
 if(substr($d,-3)=='Mp3' || substr($d,-3)=='mp3' || substr($d,-3)=='MP3' ||  substr($d,-3)=='WMA' || substr($d,-3)=
   for( $x=0; $x<count($arrayPosiciones); $x++){
    if($arrayPosiciones[$x]==$d){ echo "is the same!"; }
    .
    .
    .

echo "<br>array: ". strlen(trim($arrayPosiciones[$x]));
echo "<br>file: ". strlen(trim($d));
if( strlen(trim($arrayPosiciones[$x])) ==strlen(trim($d)) ){
   echo "===";
}
array: 30
file: 23

我尝试过这个片段,它在linux服务器上工作,但在mac上不工作。

<?php   
// In the very first line of your code
ini_set('default_charset', 'utf-8');

foreach($dir as $d){
 if(substr($d,-3)=='Mp3' || substr($d,-3)=='mp3' || substr($d,-3)=='MP3'){
     $encoded_d = htmlentities($d, ENT_COMPAT, 'UTF-8');
     $encoded_a = htmlentities($arrayPosiciones[0], ENT_COMPAT, 'UTF-8');
     if($encoded_d == $encoded_a){ 
          echo "is the same!"; 
       }else{
           echo "nope"; 
       }
    }
}

在Windows服务器上:

$ruta= getcwd();//$_REQUEST['ruta'];
$dir =scandir($ruta);
$arrayPosiciones[0] = "algo está cambiando";
foreach($dir as $d){
 if(substr($d,-3)=='Mp3' || substr($d,-3)=='mp3' || substr($d,-3)=='MP3'){
     $encoded_a = htmlentities($d, ENT_COMPAT, 'iso-8859-1'); 
     $encoded_d = mb_convert_encoding($arrayPosiciones[1], 'iso-8859-1');
    echo ($encoded_d);echo "<br>";
    echo ($encoded_a);echo "<br>";
    if($encoded_d == $encoded_a){ 
        echo "is the same!"; 
    }else{
        echo "nope"; 
    }
}
}

在我的windows中,如果我为数组中的文件设置参数url encode iso-8859-1并保留UTF-8,则服务器可以正常工作。

这很可能是由存储在文件中和存储在数组中时的不同编码引起的。使用mb_convert_encoding将它们转换为一种编码,然后进行比较。

http://php.net/manual/en/function.mb-convert-encoding.php