添加行到文本文件并保存在PHP中

时间:2017-04-15 10:40:00

标签: php

使用这个脚本,我创建了一个文本文件,根据我的需要排序,但是我需要在文件的开头添加一行来添加注释并将其保存到带有php扩展名的新文件中,怎么能我呢?

$txt = "file.txt";
    $weights = array(
"Example 1"=>200,
"Example 2"=>199,
"Example 3"=>198,
"Example 4"=>197,
"Example 5"=>196);

  $righi = file($txt);
    foreach($righi as $id => $player){
  list($name, $position) = explode(">", $player);
  $weighted[$weights[trim($position)]] = trim($player);
}

//sort by keys (reverse order ... descending)
krsort($weighted);

//output to screen
echo implode("<br>",$weighted);

//overwrite original file
file_put_contents($txt, implode(PHP_EOL, $weighted));

这是输出

<a href='http://www.example.com/Example 1.txt'>Example 1
<a href='http://www.example.com/Example 2.txt''>Example 2
<a href='http://www.example.com/Example 3.txt''>Example 3
<a href='http://www.example.com/Example 4.txt''>Example 4
<a href='http://www.example.com/Example 5.txt''>Example 5

0 个答案:

没有答案