PHP上传文件并逐行读取

PHP上传文件并逐行读取,php,html,Php,Html,我这里有一个非常奇怪的问题。我使用下面的代码通过php将一个txt文件上传到我的服务器 <form enctype="multipart/form-data" action="index.php" method="POST"> <input type="file" name="uploadfile"> <button>Submit</button> </form> 然后运行preg_匹配

我这里有一个非常奇怪的问题。我使用下面的代码通过php将一个txt文件上传到我的服务器

    <form enctype="multipart/form-data" action="index.php" method="POST">
        <input type="file" name="uploadfile">
        <button>Submit</button>
    </form>
然后运行preg_匹配以过滤阵列:

$list = preg_grep('/^[a-z]+\.com$/', $domains);
我确认$domains正在使用txt文件中的数据填充,但是当我运行print\r($list)时,它只返回“array()”

当我用FTP上传相同的文本文件并运行print\r($list)时,效果很好


我还注意到,当我用FTP客户端上传同一个文本文件时,“domains.txt”的文件大小比我用html表单上传时要小。知道这里发生了什么吗?

这可能与FTP客户端在UNIX和Windows之间自动更改行尾有关

要解决此问题,只需使用
trim
在使用
preg\u grep
之前清洁每一行即可。比如:

foreach ($domains as $key => $domain) {
    $domains[$key] = trim($domain);
}

// or better use array_walk and closures PHP 5.3+
array_walk($domains, function(&$e) { $e = trim($e); });
问题可能是因为正则表达式具有显式的线起点和线终点定位

顺便说一句,只需使用
file
函数,就可以将整个文件加载到一条语句中的行数组中

$domains = file($file);

请不要忽略“解决它们”警告。file()将提供一个逐行数组,比使用freed和explode@user1647347如果你喜欢我的答案,你可以接受:我会赌一美元,这就是问题所在……有人欠达贡一美元。谢谢你的帮助@达贡哈哈,美元正是问题所在:D
$domains = file($file);