比较sql数据库和PHP. xls文件中的两个西里尔字符串是否相等



我使用phpoffice/phpspreadsheet来处理.xlss文件。

由于文件中的列顺序可能因文件发送者而异,因此我在数据库中有一个表,其中存储发送者名称(以民事形式)和其余数据的列数。

因此,在处理文件之前,我需要比较数据库中的发送者名称是否与文件中字段的内容相同,以获得对应列的值,并仅在名称值匹配时处理文件。

尽管我100%确定它们是相同的,但无论进行什么比较,我都返回false。

1。文件处理

$allowedFileType = [
'application/vnd.ms-excel',
'text/xls',
'text/xlsx',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
];

$targetPath = './tmp/' . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
$filename = $_FILES['file']['name'];
$error = 0;
//Check if file is already uploaded
if (!empty($importFromFile->getImportedFileName($filename))) {
setEventMessages($langs->trans('FileExists'), null, 'errors');
$error++;
}

if (!$error) {
$Reader = new Xlsx();
$spreadSheet = $Reader->load($targetPath);
$excelSheet = $spreadSheet->getActiveSheet();
$spreadSheetAry = $excelSheet->toArray();
$sheetCount = count($spreadSheetAry);
foreach ($externalsales->getImportSettings() as $obj) {
$name1 = $obj->ref; //This is the name from the database
$name_row = $obj->distributor_name_row - 1;
$name_column = $obj->distributor_name_column - 1;
$name2 = $spreadSheetAry[$name_row][$name_column]; //this is the name from the file
//At this point, I need to compare returned names from database to the name in the file
}
}

2。比较

print $name1 . ' - ' . $name2;
//returns ДЕЛИВЪРИ ООД - ДЕЛИВЪРИ ООД
//You can see they are same
print strcasecmp($name1, $name2);
print strcasecmp(trim($name1), trim($name2));
print strcasecmp(mb_strtolower($name1), mb_strtolower($name2));
//all 3 return "176"
print strcmp($name1, $name2);
print strcmp(trim($name1), trim($name2));
print strcmp(mb_strtolower($name1), mb_strtolower($name2));
//all 3 return "1"
print strcoll($name1, $name2) . '<br>';
//returns "1"
$coll = collator_create( 'bg_BG' );
$res  = collator_compare( $coll, $name1, $name2 );
print $res;
//returns "1"

对于测试,我在数据库中插入了第二个记录,我不确定是否在文件中。

对于前3个选项,返回32,32,-176,对于第二个3是1,1,-1,1strcoll,-1是排序器。

感谢您的帮助。

为了检查导致错误的原因,我对两个值都使用了bin2hex()。结果显示,在来自.xls文件的字符串中有一些空白(我假设20代表html中的%20)。

输出为:

print bin2hex($name1)
//d094d095d09bd098d092d0aad0a0d09820d09ed09ed094
print bin2hex($name1)
//d094d095d09bd098d092d0aad0a0d0982020d09ed09ed0942020

因此,当我用str_replace(' ', '', $name1);删除这些空白时,if条件if ($name1 === $name2)返回true,这就是我正在寻找的。

如果有人能比我更好地解释这种差异的原因,请随时回复。

最新更新