按排序顺序对未排序的文本文件进行排序并重写为相同的文本文件
我有问题。我正在学习如何读取/写入文件,但在尝试在同一个php脚本中同时执行这两个操作时遇到了一些麻烦。我有这样的话一个文本文件,按排序顺序对未排序的文本文件进行排序并重写为相同的文本文件
Richmond,Virginia
Seattle,Washington
Los Angeles,California
Dallas,Texas
Jacksonville,Florida
我写了一个代码,以便他们进行排序,这将通过市显示排序顺序。
<?php
$file = file("states.txt");
sort($file);
for($i=0; $i<count($file); $i++)
{
$states = explode(",", $file[$i]);
echo $states[0], $states[1],"<br />";
}
?>
由此,我该如何将这些排序信息重写回到states.txt文件中?
一旦通过file的调用将文件读入阵列中。您可以通过使用fopen功能打开文件进行写入,使用fwrite写入该文件并关闭使用fclose文件句柄:
<?php
$file = file("states.txt"); // read file into array.
$fh = fopen('states.txt','w') or die("..."); // open same file for writing.
sort($file);
for($i=0; $i<count($file); $i++)
{
$states = explode(",", $file[$i]);
echo $states[0], $states[1],"<br />";
fwrite($fh,"$states[0],$states[1] <br />"); // write to file.
}
fclose($fh); // close file.
?>
尝试使用的fopen和fwrite。
$fileWrite = fopen("filePah", "w");
for($i=0; $i<count($file); $i++)
{
fWrite($fileWrite, $file[i]);
}
fClose($fileWrite);
也谢谢你。D – arrgggg 2010-04-13 14:49:06
<?php
$file = file("states.txt");
sort($file);
$newContent = "";
for($i=0; $i<count($file); $i++)
{
$states = explode(",", $file[$i]);
$newContent .= $states[0] .', '. $states[1] . PHP_EOL;
}
file_put_contents('states.txt',$newContent);
?>
打开文件,写微博,关闭它(这个假设$文件是从你的代码中的变量):
$fp = fopen('states.txt', 'w');
for($i=0; $i<count($file); $i++)
fwrite($fp, $file[$i]);
}
fclose($fp);
将$file
的内容写回t的最简单方法他将与implode
合作使用file_put_contents
。
file_put_contents("states.txt", implode($file));
塞巴斯蒂安,那简直太完美了。我所需要做的只是删除“\ n”,因为它在每个记录之间添加了额外的空白行。谢谢。 – arrgggg 2010-04-14 11:47:28
哦,我的坏。固定。 请记住,如果你喜欢它,请接受它。 :) – 2010-04-14 13:10:56
尝试是这样的:
$fo = fopen("filename", "w");
$content = "";
for ($i = 0; $i < count($file); $i++) {
$states = explode(",", $file[$i]);
$content .= $states[0] . "," . $states[1] . "\n";
}
fwrite($fo, $content);
fclose($fo);
这一点扩展,但我想这可能是有用的中小企业。我有一个m3u播放列表,只需要筛选,排序和打印特定的行。积分去魔界:
<?php
//specify that the variable is of type array
$masiv = array();
//read the file
$file = '/home/zlobi/radio/pls/all.m3u';
$f = fopen($file, "r");
while ($line = fgets($f))
{
//skip rows with #EXT
if(strpos($line, "#EXT") !== false) continue;
$text = str_replace('.ogg', ' ', $line);
$text = str_replace('/home/zlobi/radio/',' ',$text);
//add the song as an element in an array
$masiv[] = $text;
}
$f = fclose($f);
//sort the array
sort($masiv);
//pass via the array, take each element and print it
foreach($masiv as $pesen)
print $pesen.'<br/>';
?>
masiv是数组,pesen是歌曲在保加利亚:) 大写字母会先被排序。
Regads
这是迄今为止最快,最优雅的解决方案,我发现,当我有同样的问题。 如果你是在Linux上(以exec允许PHP配置),你可以做以下的(前提是你要的文件排序数值):在基本上
exec("sort -n " . $pathToOriginalFile . " > " . $pathToSortedFile);
,执行bash命令排序进行排序行数字化的文件。如果你想要一个字母排序只是排除-N(--numeric排序)选项
exec("sort -n " . $pathToOriginalFile . " > " . $pathToSortedFile);
exec("rm " . $pathToOriginalFile);
exec("mv " . $pathToSortedFile . " " . $pathToOriginalFile);
: 如果你想保持数据的原始文件做到这一点。
exec("sort " . $pathToOriginalFile . " > " . $pathToSortedFile);
对于我来说,命令花了大约3秒对服务器上文件中的1000万行进行排序。
你可以找到更多有关排序在这里http://www.computerhope.com/unix/usort.htm
希望它能帮助。
*(参考)* http://de2.php.net/manual/en/function.file-put-contents.php – Gordon 2010-04-13 14:47:07