When using league/csv to read a csv file, how could I know which csv controls have been used to parse the file ?
I made different csv files :
delimiter_colon.csv {exported from excel with colon delimiter}
delimiter_tab.csv {exported from excel with tab delimiter}
delimiter_semicolon.csv {exported from excel with semicolon delimiter}
etc...
When using
use League\Csv\Reader;
...
//Read csv from path
$csvReader = Reader::createFromPath( $CSVFile->path );
//get the current delimiter ? Nope always the default one ...
$this->delimiter = $csvReader->getDelimiter();
EDIT: What I want to know is which delimiter has been used by the current reader.
Not the delimiter in the csv file itself.
Whatever the file I use to read the csv, it always gives "," {coma}
So I'm asking here:
How to know which delimiter/enclosure were used to parse the current csv Reader ?
I've also tried using getIterator(). Get default values too.
If you are using the latest version of the library you can simply use Reader::fetchDelimitersOccurrence as explained in the documentation. But be aware that the method will only return information about the suggested delimiters you supply. Because it is not possible for the method to know which delimiter is in use. The result is only a hint that needs to be confirm by the CSV provider.
Related
Is this possible? or must one use a PHP library such as thephpleague/csv?
Example, according to PhpSpreadsheet's documentation:
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$spreadsheet = $reader->load($file);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
$writer->setUseBOM(true);
$writer->setDelimiter(',');
$writer->setEnclosure('');
$writer->setLineEnding("\r\n");
$writer->setSheetIndex(0);
$writer->save('test.csv');
As you can see this completely counter-productive as the load() method requires an actual $file to do essentially the same thing.
Is there a way to use PhpOffice\PhpSpreadsheet\Writer\Csv() without using the spreadsheet and instead using a string containing CSV data?
Thanks in advance for all positive inputs and suggestions.
You don't need to load a file, you can create an empty Spreadsheet object directly by using
$spreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
as shown in the "Hello World" example in the PHPSPreadsheet documentation
But if all you want to do is write a csv file; then you're far better using PHP's built-in fputcsv() function than a library designed to manipulate genuine spreadsheets with multiple worksheets, formatting, formulae, and working with multiple different file formats, etc.
Or for simply writing a string that's already concatenated (are you sure that you've quoted everything that needs quoting, and escaped everything that needs escaping), just use fwrite().
Counter-productive is building your own csv string and then using a spreadsheet library to write each line.
I am using a CsvReader to read a csv generated through a Cobol script that i can't access.
In my script i have this code
$file = new \SplFileObject('/path/to/my/Category_1396548812.csv');
$reader = new CsvReader($file, "~", chr(0));
var_dump($reader->count());
and it output
int(1)
The csv file contains 7 lines and it is available for download here: https://www.dropbox.com/s/ux9wgeofq4ejoj4/Category_1396548812.csv
If i create another csv from the scratch, it works properly and the method count() return the right number of rows, so i think the issue could be in the format of the generated csv, but i don't understand how to fix the problem.
Any suggestion?
Cheers
The problem was that the file generated use \r as line separator instead of a \n.
I was told that "\t" would 'tell' Excel that it is a new column data.
However, it is just an empty space and all my single line data are in ONE cell instead of seperate columns.
Did i do anything wrong?
while($row =mysqli_fetch_assoc($result))
{
$contents.=$row['idUsers']."\t";
$contents.=$row['First_Name']."\t";
$contents.=$row['Last_Name']."\t";
$contents.=$row['Email_Address']."\t";
$contents.=$row['Verified']."\n";
}
When you're creating a CSV file for MS Excel, you'll find that Excel's separator is locale specific, so what might work for one person won't necessarily work for another.
One way to try and force the issue is to use a sep=<x> line as the very first line of your CSV file; so if you initially define $content using
$contents = "sep=\t\n";
before starting your while loop, you may find that this allows MS Excel to correctly identify what separator you're using when the file you generate is loaded into Excel
And rather than "roll your own" csv format file, why don't you make use of PHP's built-in fputcsv() function, which will also handle quoting strings and generally simplify things for you
Have you tried the CSV format?
Replace your '\t' by a comma..
while($row =mysqli_fetch_assoc($result))
{
$contents.=$row['idUsers'].",";
$contents.=$row['First_Name'].",";
$contents.=$row['Last_Name'].",";
$contents.=$row['Email_Address'].",";
$contents.=$row['Verified']."\n";
}
http://en.wikipedia.org/wiki/Comma-separated_values
I am trying to create a Microsoft word document without using any 3rd party libraries. What I am trying to do is :
Create a template document in Microsoft Word
Save it as an XML File
Read this XML file and populate the data in PHP
I am able to do it so far. I would like to export it as an *.docx format. However when I do that, it is throwing an exception, when I try to open it.
Error Message : File is corrupt and cannot be opened
However, when I save it as *.doc, I am able to open the word document.
Any idea, what could be wrong. Do I need to use any libraries to export it to an docx file ?
Thanks
Docx is not backwards-compatible with doc. Docx is a zipped format: Docx Tag Info.
I would recommend you to create another template for the docx format, because the formats are so different.
Also, you might want to check that your code is writing the correct encoding. Before I put it in the correct encoding I was getting odd letters that weren't compatible when I converted it into a .docx format. To do this I implemented it in the inputstream:
InputStreamReader isr= new InputStreamReader(template.getInputStream(entry), "UTF-8");
BufferedReader fileContents = new BufferedReader(isr);
I used this with enumeration for the entry, but the "UTF-8" puts it in the right format and eliminates the odd characters. I was also getting "null" typed out at the end of some of the xml's, so I eliminated that by taking it out (I brought the contents of each file into a string so I could manipulate it anyway):
String ending = "null";
while(sb.indexOf(ending) != -1){
sb.delete(sb.indexOf(ending), (sb.indexOf(ending) + ending.length()));
}
sb was the stringbuilder I put it into. This problem may have been solved with the UTF-8, but I fixed it before I implemented the encoding, so figured I'd include it in case it ends up being a problem. I hope this helps.
"AMZN","Amazon.com, Inc.",211.22,"11/9/2011","4:00pm","-6.77 - -3.11%",4673052
Amazon.com, Inc. is being treated as 2 values instead of one.
I tried this $data = explode( ',', $s);
How do I modify this to avoid the comma in the value issue?
You should probably look into str_getcsv() (or fgetcsv() if you're loading the CSV from a file)
This will read the CSV contents into an array without the need for exploding etc.
Edit- to expand upon the point made by Pekka, if you're using PHP < 5.3 str_getcsv() won't work but there's an interesting approach here which reproduces the functionality for lesser versions. And another approach here which uses fgetcsv() after creating a temporary file.
Use a dedicated CSV library. It's been explained over and over that parsing file formats like CSV manually is asking for trouble, because you don't know all the variations of CSV and all the rules to do it right.