php - 使用 PHP 从 CSV 导入的数据破坏了隐藏字符

标签 php mysql csv laravel csv-import

我正在使用 Laravel Excel package将 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();

所有查询都会失败,即使我确定应该进行匹配也是如此。运行一些查询调试以获得查询和绑定(bind),我看到了这个(样本数据):

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 字符串一样使用它们?

非常感谢您的帮助

最佳答案

请检查下面的代码,我在这里找到了相同的代码 http://www.maatwebsite.nl/laravel-excel/docs/import#extra 检查“输入编码”部分 希望这有帮助

// When utilising a closure, you can pass the input encoding as third parameter.
Excel::load('filename.csv', function($reader) {

}, 'UTF-8');

关于php - 使用 PHP 从 CSV 导入的数据破坏了隐藏字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33498594/

相关文章:

javascript - 文件上传ajax :The body of your POST request is not well-formed multipart/form-data

php - 如果选择其他单选按钮,则更新表中的特色行 - Laravel 5.2

基于选择表单的jquery slider

mysql - SQL 请求 CREATE TEMPORARY TABLES(在终端中工作,但不通过 php)

arrays - 从 csv 文件加载矩阵 - golang

javascript - 如何从 D3.js 中的嵌套访问值?

php - 清除 Symfony 中的缓存 : `cache:clear` or `rm -rf` ?

php - 我可以在不使用本地主机服务器的情况下在本地解析 PHP 吗?

php - 如何使用zf2向mysql插入数据?

php - 编写一个 PHP 文件以从 CSV 中读取并执行 SQL 查询