preg_match_all令人困惑的失败

时间:2011-01-14 04:34:34

标签: php preg-match-all

$string = (string) file_get_contents($_FILES['file']['tmp_name']);

echo $string;

// Correctly echos string contents

preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);

print_r($matches);

// No matches

我正在解析text / csv文件并从上传的文件中获取电子邮件地址。在解析Google Contact文件时,我导出它奇怪地失败了。但是,当我只是复制echo的字符串并粘贴而不是file_get_contents结果时,它会解析并运行。

知道为什么它拒绝接受file_get_contents字符串,但如果我自己粘贴原始数据,它会起作用吗?

1 个答案:

答案 0 :(得分:0)

$_FILES['file']['tmp_name']是临时上传的文件,您应该在调用file_get_contents之前先移至目录,例如

$tmp_file = '/tmp/test.csv'
move_uploaded_file($_FILES['file']['tmp_name'], $tmp_file);
$string = file_get_contents($tmp_file);