Can't find newline in csv string - php

Using file_get_contents() I retrieve CSV data from an external URL. In this data there are breaks visible for every new record in the CSV. However, when I try to explode the lines, I only get one item in my array, containing the full CSV dataset. This is the code I use:
$data = file_get_contents('http://example.url/csvdata.asp?id=20'); // has 12 records
$rows = explode("\n", $data);
echo count($rows); // returns 1
Could this have to do with that an ASP script generates the CSV? Do they use other new line characters?

Use file and let PHP handle the new lines
file — Reads entire file into an array
You can simply do
$rows = file('http://example.url/csvdata.asp?id=20');

\n can work but there are more ways for new lines. This should work:
$lines = preg_split("/\\r\\n|\\r|\\n/", $data);

I had the same issue with mine. I found this somewhere on SO:
$rows = explode(PHP_EOL, $data);
PHP_EOL finds the end of the line.

Related

PHP write to file without last newline

I want to read a CSV data file, load it into an array, edit it and write it back to a file. I have been able to accomplish this a single iteration with examples here on Stackoverflow! Thanks.
The trouble is when I write the new data back to the file, both methods I have tried to write the edited Array back to the file add an newline at the end the file. This creates an issue when loading the CSV file data a 2nd time. The 2nd read causes an empty Index in the Array that causes an error when writing the file.
Example #1:
foreach($editArray as $row) {
$writeStuff = implode(",", $row);
fwrite($file_handle, $writeStuff);
fwrite($file_handle, "\n");
}
Example #2:
foreach ($editArray as $row) {
fputcsv($file_handle, $row);
}
This is the original csv data:
1/1/16,Yes,No
1/2/16,No,Yes
When written using the above it produces this data with the added newline:
1/1/16,Yes,No
1/2/16,No,Yes
This extra new line creates an issue when reading the file a 2nd time. I get an error on both the fputcsv() or implode(). I believe it is because the empty Index caused by the newline when I read the file the 2nd time after the first write.
I could use a for loop with a conditional on the last fwrite() in the implode() Example #1, but that would seem clunky and not the way to do it.
Maybe there is a completely different way to handle this?
This is the expected behaviour of fputcsv
fputcsv() formats a line (passed as a fields array) as CSV and write it (terminated by a newline) to the specified file handle.
Being that all lines are terminated by newline, you will have an extra blank line at the end of the file
You should apply a fix for the second read, where the last line creates issues, by checking if the line is empty before processing.
If you want to prevent adding a new line at the end of the file, you could build your data set with new lines where you need them (and where you don't) then write it once:
$writeStuff = [];
foreach($editArray as $row) {
$writeStuff[] = implode(',', $row);
}
fwrite($file_handle, implode(PHP_EOL, $writeStuff));
Also, I'm not sure how you load the file, but you could always skip empty lines - here's an example:
$editArray = file('your_filename.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Based upon the recommendation, I looked for a solution when reading and loading the file rather than when I wrote the file.
These are the solutions I came up with.
First Option:
while(! feof($file_handle)) {
$tmp = fgetcsv($file_handle);
if($tmp != NULL) {
$myArray[] = $tmp;
}
}
fgetcsv returns a NULL if the line is empty.
Second Option. Ditch the fgetcsv() for file(). It ignores the empty newline without testing.
$data_Array = file($file);
foreach($$data_Array as $key) {
$myArray[] = explode(",", $key);
}
This seems to work. Additionally the example given earlier with implode() and PHP_EOL seems to work also. I may be missing something, but these work for now.

How to delete array in text file PHP

I have a text file. I want to delete some lines with a query of search.
The array is line by line. I want to made it like http://keywordshitter.com/
The logic is,
SEARCH --> IN ARRAY --> OUTPUT IS ARRAY WITHOUT "QUERY OF SEARCH"
Code I have tried:
$fileset = file_get_contents("file.txt");
$line = explode("\n", $fileset);
$content = array_search("query",$line);
print_r($content);
MY file.txt
one
two
three
apple
map
I have used array_search but not working.
you can do search like
$fileset=file("file.txt"); // file function reads entire file into an array
$len=count($fileset);
for($i=0;$i<$len;$i++)
{
if($fileset[$i]=="query")
{
$fileset[$i]="";
break; //then we will stop searching
}
}
$fileset_improve=implode($fileset); //this will again implode your file
$handle=fopen("file.txt","w"); //opening your file in write mode
fwrite($handle,$fileset_improve); //writing file with improved lines
fclose($handle); //closing the opened file
remember this lines will make your search line blank....
if you wanna then you can arrange whole array i.e. shifting following indexed data to previous index to decrease line counts but this will increase your programming complexity.
Hope this will work for you.
Thanks
Use PHP_EOL on your explode function instead of "\n". PHP_EOL will handle the correct line break character(s) of the server platform.

Storing last line of file in an array continuously in PHP

So i have a little issue with some PHP read functionality. What I am trying to do is basically grab data into an array from a file that is being continuously updated from a python script reading values from a micro controller. So basically, the file would look something like this.
ID, Datetime, Count, Name
ID, Datetime, Count, Name
ID, Datetime, Count, Name
What i need is for it to read the new line that is being added in (eof) and store it into an array. So what i have so far is allowing read access into the file
<?php
$myfile = fopen("read.txt", "r")
For the storing the lines in an array i figured something like an array map would be efficient
$result = array();
// some loop
$parts = array_map('trim', explode(':', $line_of_text, 2)));
$result[$parts[0]] = $parts[1];
However i am not to sure on how to structure the loop to have it read the new line that is being updated in the file without exiting the loop.
while (feof($file)) {
}
fclose($file);
?>
Any help would be appreciated!!
Can you do this?
Read the lines of the file to an array using $lines = file("filename");.
Use the $lines[count($lines) - 1] to get the last line?
You can even trim off the empty lines before you wanna do this.
Trim Empty Lines
Use this function:
$lines = array_filter($lines);
Since the file is continually being appended, you'd have to read until you hit the end of file, sleep for a while to let more data be appended, then read again.
e.g.
while(true) {
while(!feof($file)) {
... process data
}
sleep(15); // pause to let more data be appended
}
However, I'm not sure if PHP will cache the fact that it hit eof, and not try again once the sleep() finishes. It may be necessary to record your current position ftell(), close the file, reopen it, then fseek() to the stored location.
I've came up with this solution
$filename = "file.txt";
$file = fopen($filename, "r");
$lines = explode("/n", fread($file, filesize($filename)));
$last = $lines[count($lines)-1];
If the file is going to get big, it could take some time to parse, so its also possible to adjust the fread() function so it only reads the last 100 characters for example.

Deleting lines from binary data

How can I delete some lines("\r\n") from a binary data:
for example if I have $data which is the content of a binary file I'm getting from my database and wanted to delete the first five lines from it.
How can this be done in php?
// Load the file into memory
$fileData = file_get_contents('myfile.bin');
// Split it by CRLF sequences
$fileData = explode("\r\n", $fileData);
// Remove the first 5 lines
$fileData = array_slice($fileData, 5);
// Turn it back into a string
$fileData = implode("\r\n", $fileData);
// Write it back to the file
file_put_contents('myfile.bin', $fileData);
Or, in one line:
file_put_contents('myfile.bin', implode("\r\n", array_slice(explode("\r\n", file_get_contents('myfile.bin')), 5)));
But if you want to do this with "binary data", I would question whether it really is binary data.
If you simply want to get rid of all empty lines at the start of the string you could use ltrim() PHP Manual ltrim but if the lines aren't empty and you know you want to get rid of exactly 5 lines then this preg_replace should do the trick:
$newData = preg_replace('/(.*)\r\n/', '', $data, 5);
[Edit:]
Come to think of it, this might not work for binary data at all but you can give it a try.

PHP and file manipulation

I was wondering if it is possible to open an xml file as a plain text file so I can read in each line and manipulate the text?
$xml_file = '/var/www/file.xml';
$contents = file_get_contents($xml_file); // Dumps the entire file into a single string
$contents = file($xml_file); // Dumps each line into an array
However, I would recommend using simplexml_load_file() (even though you said you wanted to avoid it) because there is no guarantee as to how the xml will be formatted. It could all be on a single line or formatted with line-breaks in unexpected places.
Why not use any of the XML parser/manipulator directly?
You can find those references at http://www.php.net/manual/en/refs.xml.php
If you have a nicely formatted XML file then,
$file = 'file.xml';
// get contents and normalize the newline
$xml_arr = file($file);
foreach($xml_arr as &$line){
// do your manipulation to $line
}
$ctns = implode("\n",$xml_arr);
file_put_contents($file,$ctns); // write back
To read file as array of strings use file function:
$lines = file('your_xml_file.xml');
foreach($lines as $line) {
## do the stuff for each line
}

Categories