簡體   English   中英

將每行的開始和結束字符添加到PHP中的文本文件中

[英]Adding a character beginning and end of each line to text file in PHP

我有一個 'text.txt' 文件這個文本文件有 1000 行不同的行,我想添加每一行的開頭“並添加每行的結尾”,

塊報價

請幫我

您可以使用 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.
<?php 

   $str = 'sometext';

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

   //expected output
   "sometext"
?>

您可以使用 [period] 運算符連接它們。 如何在php中連接字符串

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM