哪种方法更好?使用 PHP 对文件中的每一行进行哈希处理
这个问题是在留言板上提出的,我希望得到明确的答案和关于哪种方法在语义上更正确且资源占用更少的明智辩论。
假设我有一个文件,该文件中的每一行都包含一个字符串。我想为每一行生成一个 MD5 哈希并将其写入同一个文件,覆盖以前的数据。我的第一个想法是这样做:
$file = 'strings.txt';
$lines = file($file);
$handle = fopen($file, 'w+');
foreach ($lines as $line)
{
fwrite($handle, md5(trim($line))."\n");
}
fclose($handle);
另一个用户指出 file_get_contents() 和 file_put_contents() 比在循环中使用 fwrite() 更好。他们的解决方案:
$thefile = 'strings.txt';
$newfile = 'newstrings.txt';
$current = file_get_contents($thefile);
$explodedcurrent = explode('\n', $thefile);
$temp = '';
foreach ($explodedcurrent as $string)
$temp .= md5(trim($string)) . '\n';
$newfile = file_put_contents($newfile, $temp);
我的论点是,由于这样做的主要目标是将文件放入数组中,而 file_get_contents() 是将文件内容读取到字符串中的首选方法,file()更合适,它可以让我们删掉另一个不必要的函数explode()。
此外,通过使用 fopen()、fwrite() 和 fclose() 直接操作文件(与调用 file_put_contents() 完全相同),无需使用额外的变量来存储转换后的字符串;您将它们直接写入文件。
我的方法与替代方法完全相同 - 文件上的打开/关闭次数相同 - 除了我的方法更短且语义更正确。
你有什么要说的,你会选择哪一个?
与前两种方法相比,这应该更高效且资源消耗更少:
$file = 'passwords.txt';
$passwords = file($file);
$converted = fopen($file, 'w+');
while (count($passwords) > 0)
{
static $i = 0;
fwrite($converted, md5(trim($passwords[$i])));
unset($passwords[$i]);
$i++;
}
fclose($converted);
echo 'Done.';
This question was asked on a message board, and I want to get a definitive answer and intelligent debate about which method is more semantically correct and less resource intensive.
Say I have a file with each line in that file containing a string. I want to generate an MD5 hash for each line and write it to the same file, overwriting the previous data. My first thought was to do this:
$file = 'strings.txt';
$lines = file($file);
$handle = fopen($file, 'w+');
foreach ($lines as $line)
{
fwrite($handle, md5(trim($line))."\n");
}
fclose($handle);
Another user pointed out that file_get_contents() and file_put_contents() were better than using fwrite() in a loop. Their solution:
$thefile = 'strings.txt';
$newfile = 'newstrings.txt';
$current = file_get_contents($thefile);
$explodedcurrent = explode('\n', $thefile);
$temp = '';
foreach ($explodedcurrent as $string)
$temp .= md5(trim($string)) . '\n';
$newfile = file_put_contents($newfile, $temp);
My argument is that since the main goal of this is to get the file into an array, and file_get_contents() is the preferred way to read the contents of a file into a string, file() is more appropriate and allows us to cut out another unnecessary function, explode().
Furthermore, by directly manipulating the file using fopen(), fwrite(), and fclose() (which is the exact same as one call to file_put_contents()) there is no need to have extraneous variables in which to store the converted strings; you're writing them directly to the file.
My method is the exact same as the alternative - the same number of opens/closes on the file - except mine is shorter and more semantically correct.
What do you have to say, and which one would you choose?
This should be more efficient and less resource-intensive as the previous two methods:
$file = 'passwords.txt';
$passwords = file($file);
$converted = fopen($file, 'w+');
while (count($passwords) > 0)
{
static $i = 0;
fwrite($converted, md5(trim($passwords[$i])));
unset($passwords[$i]);
$i++;
}
fclose($converted);
echo 'Done.';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如其中一条评论所建议的那样,做对你来说更有意义的事情。因为您可能会在几个月后再次看到这段代码,并且您需要花费最少的时间来尝试理解它。
但是,如果您关心速度,那么我将创建两个测试用例(您几乎已经得到它们)并使用
timestamp
(在脚本开头使用timestamp
创建变量,然后在脚本末尾从脚本末尾的timestamp
中减去它,以计算出差异 - 运行脚本需要多长时间。)准备几个文件,我会使用大约 3 个文件,两个极端文件和一个正常文件。查看哪个版本运行速度更快。http://php.net/manual/en/function.time.php
我认为差异很小,但这也取决于您的文件大小。
As one of the comments suggests do what makes more sense to you. Since you might come back to this code in few months and you need to spend least amount of time trying to understand it.
However, if speed is your concern then I would create two test cases (you pretty much already got them) and use
timestamp
(create variable withtimestamp
at the beginning of the script, then at the end of the script subtract it fromtimestamp
at the end of the script to work out the difference - how long it took to run the script.) Prepare few files I would go for about 3, two extremes and one normal file. To see which version runs faster.http://php.net/manual/en/function.time.php
I would think that differences would be marginal, but it also depends on your file sizes.
我建议在处理时编写一个新的 临时文件输入一。完成后,用临时文件覆盖输入文件。
I'd propose to write a new temporary file, while you process the input one. Once done, overwrite the input file with the temporary one.