Tasnifsiz metin dosyasını sıralamak ve sıralı düzende aynı metin dosyası yeniden

7 Cevap

Bir sorum var. I / yazma dosyaları okumak için öğrenme, ama aynı php komut aynı anda hem yapmaya çalışıyor biraz sorun yaşıyorsanız sürecinde duyuyorum. Ben, bu gibi sözlerle bir metin dosyası var

Richmond,Virginia
Seattle,Washington
Los Angeles,California
Dallas,Texas
Jacksonville,Florida

Ben sırayla bunları sıralamak ve bu City sıralama düzeninde görüntüler bir kod yazdı.

<?php
$file = file("states.txt");
sort($file);
for($i=0; $i<count($file); $i++)
{
  $states = explode(",", $file[$i]);
  echo $states[0], $states[1],"<br />";
}
?>

Bu itibaren, nasıl states.txt dosyasına geri bu sıralanan bilgi yazabiliriz?

7 Cevap

Fopen ve fwrite kullanmayı deneyin.

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

for($i=0; $i<count($file); $i++)
{
    fWrite($fileWrite, $file[i]);
}
fClose($fileWrite);
<?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

Böyle bir şey deneyin:

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

Bu ancak ben konuşturmamak için yararlı olabileceğini düşündüm, biraz uzun olduğunu. Ben bir m3u çalma listesi var ve sadece belirli satırları, süzülmüş sıralanır ve baskılı gerekir. Kredi Şeytan gidin:

<?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 is array, pesen is song in Bulgarian :) CAPital letters are sorted first.

Regads

Eğer bir çağrı tarafından diziye dosya okuma yapılır sonra file. Sen, fopen işlevini kullanarak yazmak için dosyayı açmak fwrite kullanarak dosyaya yazma ve kullanarak dosya tanıtıcısı kapatabilirsiniz 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.
?>

, Dosyayı açmak için dosyaya yazma, bunu kapatın (bu $ dosyayı varsayar kodundan değişken):

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

Ve bakın http://php.net/manual/en/function.fwrite.php