简体   繁体   English

同一字符串的长度不同

[英]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 . 第一个值是从excel sheet(xls)获取的,第二个值是在执行Select查询后从Mysql表获取的,我无法执行比较,因为它们的长度不同。

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

How can i make it to have a same length so that i can compare it. 我怎样才能使它具有相同的长度,以便我可以比较它。

Not a direct answer to your question but let's first find out what's really behind that string(19) "US5922335A" . 这不是您问题的直接答案,但让我们首先了解一下string(19) "US5922335A"的真正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. 编辑:您的数据似乎是小端UCS-2 / UTF-16编码的。

i am getting as 我越来越
 string(22) "EP2153814A1" string(10) "US5922335A" 字符串(22)“ EP2153814A1”字符串(10)“ US5922335A”\n45 00 50 00 32 00 31 00 35 00 33 00 38 00 31 00 34 00 41 00 31 00 45 00 50 00 32 00 31 00 35 00 33 00 38 00 31 00 34 00 41 00 31 00\n55 53 35 39 32 32 33 33 35 41 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. 由于该字符串值的数据源是Excel工作表,因此最简单的解决方案可能是使用mb_convert_encoding()更改编码。

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

I will bet that the Mysql table defines the column as a char and not a varchar . 我敢打赌,Mysql表将列定义为char而不是varchar If so, it means that the first value has trailing spaces. 如果是这样,则意味着第一个值具有尾随空格。

You need to remove trailing spaces before doing the equality check. 在进行相等性检查之前,您需要删除尾随空格。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM