I am writing array values to a CSV file using PHP. In the array values, I have included a line break using \n. After the array values are updated, I am using the implode function as below.
$newLine[] = $row[$i].",";
$newLine[] = "\n";
$csv2 [] = implode(" ", $newLine);
However, while writing to the CSV file, an extra space gets appended to the front of the line. This is causing me some problems in display. I want to eliminate the space in front of the line while it is getting written. I tried to do the below.
$line1 = str_replace(' .','.',$line);
However, I am not able to write without the space in beginning to the CSV file.
You don't need to use spaces at all:
$newLine[] = $row[$i].",";
$csv2 [] = implode("\n", $newLine);
$csv2[] = trim ( implode("\n", $newLine) , "\n");
This should work since it removes only line breaks at the beginning and end of the string.
Related
This is a really odd behavior that I can't explain. I have a CSV file that I'm trying to format. The lines could have trailing ','s that I want to remove.
$lines = explode(PHP_EOL, $csv);
$csv = '';
foreach($lines as $line) {
$csv .= trim($line, ',') . PHP_EOL;
}
The trim is not doing anything and just returning the line back as it is. Just to make sure I copied a line from the csv trim("a,b,c,d,,", ','); which works fine. Can anyone tell me why the above code won't work?
If the CSV file was created on a different operating system, it may use different line breaks than PHP_EOL. So trim any line break characters in addition to commas.
foreach($lines as $line) {
$csv .= trim($line, ",\r\n") . PHP_EOL;
}
Don't manually edit the CSV file. Parse it into an array, then edit the array. Then you can write the modified array back to a CSV file.
You can use fputcsv to write the data to a file, or str_putcsv (a custom function).
$newData = [];
$data = array_map('str_getcsv', $lines); // parse each line as a CSV
foreach ($data as $row) {
$row = array_filter($row); // remove blank values
// for some dumb reason, php has `str_getcsv` but not `str_putcsv`
// so let's use `str_putcsv` from: https://gist.github.com/johanmeiring/2894568
$newData[] = str_putcsv($row);
}
$newData = implode(PHP_EOL, $newData);
I have a text file with line breaks that I have already imploded in to single line and added commas to the end of every line.
I need to remove every 36th comma from the line and add line break as well.
this is my code:
$lines = file("filename.txt", FILE_IGNORE_NEW_LINES);
$comma_separated = implode(",", $lines);
$lines2 = preg_replace('/(?:[^,]*,){36}/', '$0\r\n', $comma_separated);
I have always had trouble with coding. My brains just don't go that way. I try my best, and I am 100 % sure I have searched and read the solution, but I have not understood it.
I would rather do something like this.
Instead of a regex that will eventually fail, add a string to every 36 line before you implode.
This string can easily be found with a simple str_replace() to remove and add the line break.
$lines = file("filename.txt", FILE_IGNORE_NEW_LINES);
For($i=0; $i<count($lines);){
$lines[$i] .= "something";
$i = $i+36;
}
$comma_separated = implode(",", $lines);
// String replace something to line break.
$result = str_replace("something,", "<br>", $comma_separated);
Edit sorry the string replace of course needs to go after the implode.
Sorry for the confusion.
$lines = file("filename.txt", FILE_IGNORE_NEW_LINES);
For($i=0; $i<count($lines);){
//your code
if($i%36==0){
trim($line,',');
}
}
For storing values in a .txt file I use this code:
file_put_contents('data/vote_result.txt', implode(',', $results));
and for reading i use this:
$results = explode(',', file_get_contents('data/vote_result.txt'));
The content of vote_result.txt looks like this: 0,1,2,3
How can I store a second line in the same .txt file so that the content looks like this:
0,1,2,3
0,1,2,3
and how can I read that second line?
Read second line:
$myFile = "data/vote_result.txt";
$linesArray = file($myFile);
echo $linesArray[1]; //line 2
If you want to append a line to file, use FILE_APPEND flag in file_put_contents and concatenate "\n" with implode.
file_put_contents('data/vote_result.txt', implode(',', $results)."\n", FILE_APPEND);
Besides that you should use a database like MySQL for this, you can use the file function. Example:
$data = file('file.txt');
print $data[1]; // printing out the second line
This given you can simply add new lines just by adding a new entry in the array and then implode it with the newline character and save it via the file_put_contents function.
$content = implode("\n", $data);
file_put_contents('file.txt', $content);
In PHP, how can one edit a text file and save it so that everything after the first space is removed?
In other words, so that each line only has its first word?
For example, if the text file looked like this:
Adi NNP
Adia NNP
Adios NNP FW
Adios-Direct NNP
Adios-On NNP
Adios-Rena NNP
Adios-Trustful NNP
Adirondack NNP
Adirondacks NNPS
Adjoining VBG
Adjournment NN
after executing the script, the text file would look like this:
Adi
Adia
Adios
Adios-Direct
Adios-On
Adios-Rena
Adios-Trustful
Adirondack
Adirondacks
Adjoining
Adjournment
How I would approach this would be to open the file, read it in, and take each line and store it in an array. Then replace everything after the first space with nothing. And lastly, save the edited array to a new file.
Is there a better way to do it than that?
All I know how to do in the above method is everything except the last two tasks. I would do it like so:
$file = array();
$lines = file('file.txt');
foreach($lines as $line){
array_push($file, $line);
}
// now travel through $file and replace everything after first space with nothing
// travel though $file again, but write each element as a new line in a .txt file
You can use explode() to separate the line by spaces. Then you can immediately write the string back to a file, no second loop is required:
$file = array();
$lines = file('file.txt');
$new_file = fopen('new.txt', 'w+');
foreach($lines as $line){
$bits = explode(' ', $line);
fwrite($new_file, $bits[0] . PHP_EOL);
}
fclose($new_file);
You can do it in the same line: just replace array_push($file, $line) with...
$file[] = strtok($line, ' ');
It can be written even more compact with help of array_map:
$lines = array_map(function($line) {
return strtok($line, ' ');
}, file('file.txt'));
... or you can write it back immediately, as shown in #hek2mgl answer.
You can bypass arrays entirely and do this with a simple regular expression:
// Read in contents into a variable
$data = file_get_contents('input.txt');
// Drop the space and everything after on each line
$data = preg_replace('/ .*$/m', '', $data);
// Dump contents to file (change this to input.txt if you want to overwrite the file)
file_put_contents('output.txt', $data);
I have a PHP page which accepts input from user in a text area. Multiple strings are accepted as input from user & would contain '\n' and I am scanning it as:
$data = explode("\n", $_GET['TxtareaInput']);
Each string should be moved into the text file with new line character separation. This is the code I am using now and it separates each string with a '^M' character:
foreach($data as $value){
fwrite($ourFileHandle, $value);
}
Is there anyway I can get each string followed by a carriage return?
You can simply write it back using implode:
file_put_contents('file.csv', implode(PHP_EOL, $data));
Try this:
$data = explode("\n", $_GET['TxtareaInput']);
foreach($data as $value){
fwrite($ourFileHandle, $value.PHP_EOL);
}
If you want to add new lines, then why are you first removing them?
$data = explode("\n", $_GET['TxtareaInput']);
Keep only this line:
fwrite($ourFileHandle, $data);
It will write your data to the file as it was received.
If you want to replace all new lines by carriage returns before writing to file, use this code:
fwrite($ourFileHandle, str_replace("\n", "\r", $data));