我正在使用Laravel Excel 包将 CSV 文件导入我的应用程序,并尝试将其数据与我的数据库进行匹配。
到目前为止的代码是:
Excel::load($file, function($reader) { //Load it into Excel package
$reader->each(function($row) { //Loop through row by row
$firstname = $row->forename;
$surname = $row->surname;
$firstname = str_replace('"', '', $firstname); //Remove the enclosure "
$surname = str_replace('"', '', $surname);
$client = DB::table('clients')->where('firstname', $firstname)->where('surname', $surname)->first();
if ($client!=null) {
print $client->id."<br><br><br>";
} else {
print "Fail<br><br><br>";
}
});
})->get();
即使我确定应该进行匹配,所有查询都会失败。运行一些查询调试来获取查询和绑定,我看到了这个(示例数据):
string 'select * from `clients` where `firstname` = ? and `surname` = ? limit 1' (length=71)
array (size=2)
0 => string 'E�l�i�z�a�b�e�t�h' (length=17)
1 => string '��W�e�s�s�o�n��' (length=15)
如您所见,从 CSV 中提取的所有字符串在每个字符后都有一个 �。
我认为这可能是某种编码问题?我是否需要转义或处理 CSV 文件中的字符串,然后才能像普通 PHP 字符串一样使用它们?
非常感谢您的帮助