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

Php 比较两个字符串,显然它们相等,php,Php,我试着比较两个字符串。我从数组中获取第一个字符串,第二个读取目录。问题是,我试图比较数组名与读取的文件名是否匹配,并且是否相同。字符串为: algo está cambiando.mp3 它存在于数组和目录中 我将打印以下消息以进行检查: 数组长度:30 algo estácambiando.mp3//数组元素 文件长度:23 algo estácambiando.mp3//文件 我用这个代码做比较,总是抛出我是假的。我不知道,因为他们是不同的 echo "<br>array l

我试着比较两个字符串。我从数组中获取第一个字符串,第二个读取目录。问题是,我试图比较数组名与读取的文件名是否匹配,并且是否相同。字符串为:

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)  ;
echo“
数组长度:“.mb_strlen(($arrayPosiciones[$x]))。" ". ($arrayPosiciones[$x]); echo“
文件长度:”.mb_strlen($d))。($d);
我想这是口音的问题。我试过很多方法。即使是我为具有波浪线的项目放置htmlentities的数组。使用htmlentities,单词显示正确

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

for($i=0;$i这很可能是由于存储在文件和数组中时的不同编码造成的。请使用mb_convert_编码将它们转换为一种编码,然后进行比较


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


我试过这个代码片段,它可以在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"; 
       }
    }
}

我试过这个代码片段,它可以在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"; 
       }
    }
}


可能是字符编码--
strlen()
返回字节,而不是字符;在这里可能是一个更好的选择。但是,对于一个简单的“a”,7个字节的差异是很大的。你确定结尾不仅仅有一些你看不到的空格吗?strlen(trim($arrayPosiciones[$x])有什么作用
告诉你?你的字符串来自哪里?你对它们的字符编码有影响吗?@MattGibson我更正了,但仍然有问题。谢谢you@MattGibsonecho“
数组:.strlen(trim($arrayPosiciones[$x]));echo“
文件:.strlen(trim($d));if(strlen(trim($arrayPosiciones[$x])==strlen(trim($d)){echo”==“;}@MattGibson数组:30文件:23好的,让我们看看这些字符串中到底是什么。
echo bin2hex($d);
数组值也是如此。它可能是字符编码--
strlen()
返回字节,而不是字符;在这里可能是一个更好的选择。但是,对于一个简单的“a”来说,7个字节的差异很大。你确定结尾没有空格吗?
strlen(trim($arrayPosiciones[$x])
向你展示了什么?你的字符串来自哪里?你对它们的字符编码有影响吗?@MattGibson我更正了,但仍然有问题。谢谢you@MattGibson回声“
数组:”.strlen(修剪($arrayPosiciones[$x]);echo“
文件:”.strlen(trim($d));if(strlen(trim($arrayPosiciones[$x])==strlen(trim($d)){echo“==”;}@MattGibson数组:30文件:23好的,让我们看看这些字符串中到底是什么。
echo bin2hex($d);
数组值也是一样。谢谢你的回答。但是为了测试你的文件,你使用了accents?。是的,我使用这个文件名“algo estácambiando.mp3”进行了测试在你的服务器上工作?我正在尝试。我的文件在本地。我要做的是读取一个目录,将数据存储在一个数组中。然后将它们与目录进行比较。你使用的是windows还是linux服务器?谢谢回答。但是为了测试你的文件,你使用了口音?是的,我使用这个文件名“algo estácambiando.mp3”进行了测试在您的服务器上工作?我正在尝试。我的文件在本地。我要做的是读取一个目录,将数据存储在一个数组中。然后将它们与目录进行比较。您使用的是windows还是linux服务器?
$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"; 
    }
}
}