2010-04-13 22 views
0

我有问题。我正在学习如何读取/写入文件,但在尝试在同一个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文件中?

+0

*(参考)* http://de2.php.net/manual/en/function.file-put-contents.php – Gordon 2010-04-13 14:47:07

回答

0

一旦通过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. 
?> 
+0

非常感谢您的快速回复。 :D – arrgggg 2010-04-13 14:43:14

+0

顺便说一句,我不在我的电脑上,所以我现在不能测试它,但我会在以后。另外,如果我想按降序执行此操作,我假设我可以更改排序($ file); rsort($ file);那应该有效? – arrgggg 2010-04-13 14:48:11

+0

是的,您需要使用rsort($文件)以相反的顺序对文件内容进行排序。 – codaddict 2010-04-13 14:50:38

1

尝试使用的fopen和fwrite。

$fileWrite = fopen("filePah", "w"); 

for($i=0; $i<count($file); $i++) 
{ 
    fWrite($fileWrite, $file[i]); 
} 
fClose($fileWrite); 
+0

也谢谢你。D – arrgggg 2010-04-13 14:49:06

1
<?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); 
?> 

PHP: file_put_contents

+0

我已经看到了这个功能,我也会尝试。谢谢。:D – arrgggg 2010-04-13 14:56:59

+0

我也删除了“
”,因为在.txt文件中你应该使用PHP_EOL(“\ n”或“\ r \ n”)。 – 2010-04-13 15:01:50

+0

啊,没错,“
”用于html。谢谢。 – arrgggg 2010-04-13 15:03:48

0

打开文件,写微博,关闭它(这个假设$文件是从你的代码中的变量):

$fp = fopen('states.txt', 'w'); 
for($i=0; $i<count($file); $i++) 
    fwrite($fp, $file[$i]); 
} 
fclose($fp); 

而看到http://php.net/manual/en/function.fwrite.php

+0

我已经检查过php .net,但他们没有显示如何排序和写回到同一个文件。我已经学会了如何写入文件,但我不知道如何在一个php脚本中同时执行这两个操作。 – arrgggg 2010-04-13 15:00:58

+0

ummm ...打开文件读取,读取文件,关闭文件(你用'file()'完成所有操作),排序,打开文件进行写入,写入文件,关闭文件(最后三个步骤可以完成通过'file_put_contents()',就像Sebastian P.指出的那样) – orithena 2010-04-16 11:28:38

4

$file的内容写回t的最简单方法他将与implode合作使用file_put_contents

file_put_contents("states.txt", implode($file));
+0

塞巴斯蒂安,那简直太完美了。我所需要做的只是删除“\ n”,因为它在每个记录之间添加了额外的空白行。谢谢。 – arrgggg 2010-04-14 11:47:28

+0

哦,我的坏。固定。 请记住,如果你喜欢它,请接受它。 :) – 2010-04-14 13:10:56

1

尝试是这样的:

$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); 
+0

但是排序部分在哪里?或者我只是添加了代码,然后是我的代码? – arrgggg 2010-04-13 14:54:58

+0

你只需在我的之前添加你的部分,它应该工作好吧 – KotuS 2010-04-13 15:03:57

+0

非常感谢。我也会试试这个。 – arrgggg 2010-04-13 18:08:56

1

这一点扩展,但我想这可能是有用的中小企业。我有一个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

0

这是迄今为止最快,最优雅的解决方案,我发现,当我有同样的问题。 如果你是在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

希望它能帮助。