Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对未排序的文本文件进行排序,并按排序顺序重写到同一文本文件_Php - Fatal编程技术网

Php 对未排序的文本文件进行排序,并按排序顺序重写到同一文本文件

Php 对未排序的文本文件进行排序,并按排序顺序重写到同一文本文件,php,Php,我有个问题。我正在学习如何读/写文件,但尝试在同一个php脚本中同时读/写文件并没有什么困难。我有一个文本文件,上面有这样的文字 Richmond,Virginia Seattle,Washington Los Angeles,California Dallas,Texas Jacksonville,Florida 我编写了一个代码来按顺序对它们进行排序,这将按城市的排序顺序显示 <?php $file = file("states.txt"); sort($file); for($i=

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

一旦通过调用将文件读入数组中。您可以使用函数打开要写入的文件,使用写入文件,并使用关闭文件句柄:


尝试使用fopen和fwrite

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

for($i=0; $i<count($file); $i++)
{
    fWrite($fileWrite, $file[i]);
}
fClose($fileWrite);
$fileWrite=fopen(“filePah”、“w”);
对于($i=0;$i


打开文件、写入文件、关闭文件(假设$file是代码中的变量):

$fp=fopen('states.txt','w');

对于($i=0;$i将
$file
的内容写回文件的最简单方法是与协作使用


文件内容(“states.txt”,introde($file));尝试以下方法:

$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);
$fo=fopen(“文件名”,“w”);
$content=“”;
对于($i=0;$i
这有点扩展,但我认为它可能对smb有用。我有一个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/>';

?>

马西夫是数组,佩森是保加利亚语的歌曲:) 大写字母先排序


Regads

这是我遇到同样问题时找到的最快、最优雅的解决方案。 如果您在Linux上(PHP配置中允许exec),则可以执行以下操作(前提是您希望对文件进行数字排序):

基本上,执行bash命令对文件中的行进行数字排序。 如果要将数据保留在原始文件中,请执行以下操作:

exec("sort -n " . $pathToOriginalFile . " > " . $pathToSortedFile);
exec("rm " . $pathToOriginalFile);
exec("mv " . $pathToSortedFile . " " . $pathToOriginalFile);
如果需要字母排序,只需排除-n(--numeric sort)选项

对我来说,这个命令花了大约3秒钟在服务器上对文件中的1000万行进行排序

您可以在这里找到有关排序的更多信息


希望能有所帮助。

非常感谢您的快速响应。:d顺便说一句,我现在不在计算机上,所以我现在无法测试它,但我会稍后再测试。另外,如果我想按降序进行测试,我假设我可以将sort($file);更改为rsort($file);这应该可以工作吗?是的,您需要使用rsort($file)将文件内容按相反顺序排序。如果我这样做了,我的错误…但我不认为我这样做了。无论如何,似乎我需要从您的示例中修复,以摆脱“
”并插入“/n”,因为我们正在写入文本文件。谢谢。嗯…我只是单击以投票赞成,但我需要更多的声誉来完成此操作。((参考)但是排序部分在哪里..或者我只是在我的代码后面添加代码吗?你只需在我的代码之前添加你的部分,它应该可以正常工作。非常感谢。我也会尝试这个。我看到了这个函数。我也会尝试。谢谢。我还删除了“
”,因为在.txt文件中,你应该使用PHP\u EOL(“\n”或“\r\n”)。我检查了php.net,但它们没有显示如何对同一文件进行排序和写回。我学习了如何写入文件,但我不确定如何在一个php脚本中同时执行这两项操作。嗯……打开文件进行读取,读取文件,关闭文件(您使用
文件()
一步完成了所有操作),排序,打开文件进行写入,写入文件,关闭文件(正如Sebastian P.所指出的,最后三个步骤可以通过
文件放置内容()
完成)。Sebastian,那太完美了。我所要做的就是删除“\n”,因为它在每个记录之间添加了额外的空行。谢谢。 file_put_contents("states.txt", implode($file));
$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);
<?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/>';

?>
exec("sort -n " . $pathToOriginalFile . " > " . $pathToSortedFile);
exec("sort -n " . $pathToOriginalFile . " > " . $pathToSortedFile);
exec("rm " . $pathToOriginalFile);
exec("mv " . $pathToSortedFile . " " . $pathToOriginalFile);
exec("sort " . $pathToOriginalFile . " > " . $pathToSortedFile);