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

时间:2016-04-08 20:47:43

标签: php

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

algo está cambiando.mp3

它出现在数组和目录中。

我打印以下消息进行检查:

数组长度:30algoestácambiando.mp3//数组元素

文件长度:23algoestácambiando.mp3// file

我正在使用此代码进行比较,并且总是抛弃我是假的。我不知道,因为他们是不同的。

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

我已经尝试过这个代码片段并且可以在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"; 
    }
}
}

如果我在数组中设置参数url-encode iso-8859-1并保留UTF-8,那么在我的Windows服务器上工作正常。

相关问题