在PHP的文本文件中添加每行的开头和结尾的字符

时间:2018-08-16 07:18:23

标签: php

我有一个'text.txt'文件,该文本文件有1000条不同的行,我想为每行添加一个beginnig,并为每行添加末尾,

  

报价单

请帮助我

3 个答案:

答案 0 :(得分:1)

您可以使用file打开文件并将其设置为每行一个数组。
然后循环播放并添加",然后再次保存。

$arr = file("text.txt");
foreach($arr as &$line){
    $line = '"' . $line . '"';
}
unset($line); //just in case and a good thing to remember in other projects
file_put_contents("text.txt", implode(PHP_EOL, $arr)); // write file with new line separating the array items.

答案 1 :(得分:0)

<?php 

   $str = 'sometext';

   echo '"'.$str.'"';

   //expected output
   "sometext"
?>

您可以使用[period]运算符连接它们。 How to concat string in php

答案 2 :(得分:0)

//open file
    $myfile = fopen("logs.txt", "a") or die("Unable to open file!");

    $txt = "user id date";
    $txt = "'".$txt."'"; //add concatenation here

//write
    fwrite($myfile, "\n". $txt);
    fclose($myfile);
相关问题