zan zan - 2 months ago 19
MySQL Question

Different length for same string

I am getting two different count values for same string

$val1 = $data->sheets[0]['cells'][5][1];//thise value is from excel sheet
$val2 = $obj->number; // this is the value from database

var_dump(trim($val1)) , var_dump(trim($val2))

string(19) "US5922335A" , string(10) "US5922335A"


first values is getting from excel sheet(xls)and The second value is am getting from Mysql table after performing Select query
, i am unable to perform comparision like as both are having different length .

if($val1==$va2)
{
echo "Found it ";
}


How can i make it to have a same length so that i can compare it.

Answer

Not a direct answer to your question but let's first find out what's really behind that string(19) "US5922335A". Please try

foreach(array($val1,$val2) as $v ) {
    for($i=0; $i<strlen($v); $i++) {
        printf('%02x ', ord($v[$i]));
    }
    echo "<br />\n";
}

if($val1==$val2) {
    echo "Found it ";   
}

and add the output which should be something like

55 53 35 39 32 32 33 33 35 41 <br />
55 53 35 39 32 32 33 33 35 41 <br />

to your question.


edit: Your data seems to be little endian UCS-2/UTF-16 encoded.

i am getting as
string(22) "EP2153814A1" string(10) "US5922335A"
45 00 50 00 32 00 31 00 35 00 33 00 38 00 31 00 34 00 41 00 31 00
55 53 35 39 32 32 33 33 35 41

Since your data source for that string value is an Excel sheet the easiest solution is probably to use mb_convert_encoding() to change the encoding.

$val1 = $data->sheets[0]['cells'][5][1];
$val1 = mb_convert_encoding($val1, 'ISO-8859-1', 'UTF-16LE');