I know the names of the columns I want to keep in the .csv file, but I am interested in removing all columns in the .csv which I don't recognize. How can php do this?
(The names of the columns are defined in the first row of the file.)
In PHP use fgetcsv() to read in your CSV file, ignore the columns you want and then write back to a CSV file using fputcsv();
Look at fgetcsv and fputcsv. Read the contents, write the columns/data back out that you want.
Related
i have a html dynamic html table, i want to download this table as excel file.And after downloading i could make some changes in it and again upload it to a folder where it would change the database data according to fields..I was able to download the html table to an excel file, but have no idea how after changing the contents in the excel file would update the database according to it.I have heard of tongue twisters but this nerve twisting.
You may want to take a look at the PHPExcel library, available at phpexcel.codeplex.com.
If you want to do this with JS (I not recommend) you could copy/paste the excel content to a textarea in your HTML and then with JS you can do:
var temp = document.getElementById("yourtextarea").value.split(" ");
Since excel by default separates columns with '(tab)' you can explode it and save in the DB. You will also have to explode the newlines. It could be \r\n or just \n depending on your OS. After doing so, you can commit it to the database, row by row using Ajax.
If you can do it with PHP then would be much more easier. You could you explode() using newline at first (to break lines) and tab at finally (to break columns).
I have an excel file with a large amount of strings in a column. I want to add these to an array in a php file, so I want each cell in the column to be enclosed in apostrophe's and insert a comma between each. I tried copying the excel column to word and doing a find and replace in word but it uses a different apostrophe type. Any ideas?
Export the Excel file to .csv, then you can either learn to read .csv files from PHP, e.g., from this tutorial, or do something more pragmatic like open the file in a text editor like BBEdit and select the column you want using block-select mode, discarding the rest of the document.
I'm not so sure about the """ but I used a similar method to create SQL queries from cells.
="""&A1&""","""&B1&""", ...
As the title says, I am trying to open a CSV file that is comma delimited and I need to update specific part of the CSV string. Is that possible? I have looked into fseek but that only let's me count the number of bytes from the BOF so that won't work as each CSV would be different.
I was thinking if it was possible if I set the specific part I want to change with something like "CHANGEME" and have PHP look for that string/character and replace that with whatever I need.
Possible?
Try fgetcsv (documentation) or str_getcsv.
Maybe these functions will help.
This would be one of my first PHP projects.
Where to start in PHP for a script that rearranges tab delimited data.
I have a tab delimited data text file that needs to be rearranged periodically. Some entire columns would have to move to different parts of the text file, and some headers of columns need to be renamed as well. Basically my tab delimited data file is being converted to match another tab delimited data set to fit a system. (Actually the file is an Amazon.com SELLER CENTRAL Order Reports tab delimited text file that is in a different format from tab delimited text files downloaded from Amazon.com's own MARKETPLACE Order Reports)
I hope someone can give me some direction as to where I would start writing code or how to make this script work...
You can read in the TSV file using PHP's fgetcsv(), using "\t" as the delimiter.
This gives you an array of columns that you can manipulate as you require. Once done, you can then write the array as a line in a new file using fputcsv()
Edit: As an example, the process would go something like
Open file handles (using fopen()) to your existing data file (source) and the new destination file
Read in the source file line-by-line using fgetcsv()
Create a new array of columns using the values returned by fgetcsv() in the order required
Write the new array to the new file using fputcsv()
Once done, close both file handles
Surely you have to know here
To match the TAB inside php you use the string: "\t"
Next you will need the explode function to obtain the array from one row of the file
next you'll need foreach cycling among the array elements to analyse the data
of course the Arrays in PHP will always help you
and at the end when you'll need produce the final TAB delimited file you'll need the implode
Of course take a look at string and array functions in PHP.
Parse the data using fgetcsv() and use array_multisort(). If you need an example, there's one available on the multisort manual page.
I'm trying to write a PHP script which will handle sorting of a CSV file by one or multiple columns and outputting the result to another file.
Is there a way to sort the CSV file without loading it entirely into memory?
No there is no reasonable way. You need the data in memory to compare and write in a file.
You could try a bubblesort if you know the length of each line. Read one line of origin and last line of a new "ordered" file. Compare them and append or prepend in new file. After this iteration do again with the new file as origin until it is sorted.
You should use a database like MySQL.