使用php按行导入txt文件的一个坑

岁月你别催,该来的我不推。 岁月你别催,走远的我不追。

问题产品原因: windows 和 linux 换行符不一致; 所以必须存入数据库之前对一些不可见的符号过滤;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$file = $request->file('femails');
$emails = file_get_contents($file->getRealPath());
$emails = explode(PHP_EOL, $emails);
$ids = [];
foreach ($emails as $email) {
$result = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $email);
$recipient = Recipient::firstOrCreate(
[
'email' => trim($result)
],
[
'is_use' => 1
]
);
array_push($ids, $recipient->id);
}