yavg
yavg

Reputation: 3051

Compare two string apparently they are equal

I'm trying to compare 2 string. I get the first string get from an array, the second I get to read a directory. The problem is that I am trying to compare the array name matches that of the file that reading and are identical. the string is:

algo está cambiando.mp3

It is present in the array and directory.

I put to print the following messages to check:

array length:30 algo está cambiando.mp3 // array element

file length:23 algo está cambiando.mp3 // file

I'm doing with this code to compare and always throws me is false. I do not know because they are different.

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

I think that is the problem of accents or something. I have tried in many ways. Even the array I put the htmlentities for items that have a tilde. with htmlentities the word displays correctly.

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

What I can do? I'm iterating a directory and compare the current file is equal to this in the $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

Upvotes: 0

Views: 92

Answers (2)

cesare
cesare

Reputation: 2118

I have tried this snippet and works on a linux server, but not in 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"; 
       }
    }
}

on a Windows server:

$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"; 
    }
}
}

In my windows server works correctly if I set the the parameter url-encode iso-8859-1 and keep UTF-8 for the file in array.

Upvotes: 1

dors
dors

Reputation: 1152

Most likely this is caused by different encodings while storing in the file and while in the array. Use mb_convert_encoding to convert them to one encoding and then compare.

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

Upvotes: 1

Related Questions