Apologies for the complete geekery of this post.
I have some data in a file and I would like to manipulate it using PHP to produce an HTML output. Not being very familiar with file-handling, can anyone suggest how I get the data into a usable form?
The input file contains (almost) fixed width data in the form:
texttexttexttexttext ##### textext ### texttextext ###
where texttexttext is a string and #### is a number.
I’d like to import the file into a two-dimensional array, the first part containing the text (trimmed of spaces to the right), with the second containing the number (with any whitespace either side trimmed). The text field is 60 wide and the number field is 8 wide.
Does anyone have any handy code to do this, please? I’ve had a Google and can’t find what I’m after. It’s not so much the trimming as getting the two bits of data into different parts of the array. Thanks.
So it’s not fixed width and it doesn’t have any kind of delimiter (tab or comma etc). Um… are there loads of records? The best thing I can think of is to try importing it into Excel first. If there’s any kind of delimiter it should be able to cope. Otherwise it would need either manual manipulation to add a delimiter, or possibly a find-replace action in, say, Notepad. Or actively make it fixed width manually. Excel will trim the blank spaces for you if need-be.
Are you accessing an sql database with your php or directly accessing the file you have? Once the file is cleaned up (in Excel, say) it should be a doddle to read it into an sql database. Otherwise I beleive you can configure PHP to read from an ASCII text array or even directly from an Excel sheet. It’s pretty flexible.
Thanks. It’s fixed width give or take one character – for some reason the SQL which produced the file didn’t quite get the columns right – so splitting it at the 60th character and trimming the white space around the values would be OK. What I can do, though, it rerun the SQL and add a delimiter if that makes the job easier. It’s a big file so it’s not feasible to do anything manual to it.
The data is in a text file and I don’t want to go via another SQL database – just open the text file via PHP and produce an HTML page based on its contents. But to do that I need to parse the data into a manipulable (if that’s a word) form. So I’d like to read the file into an array on which I can use PHP to produce my HTML result.
If it’s already in an SQL database can’t you connect to it directly? Not a local datasource?
Anyway – I know PHP can handle csv files without problems. Quick Google found this:
http://www.phpclasses.org/browse/file/8489.html
and:
http://uk.php.net/fgetcsv
But I’m sure there are others.
Yep, that’s it – the original SQL source is a proprietary database on a non-PHP server.
The second link looks good. I’ll try that out (probably on Monday) and see how it goes.
Thanks.
I reran the SQL to make a CSV file and trimmed out the excess spaces via search and replace in Notepad. PHP worked great – thanks.
Very nice. You could have always used the trim() function to remove any whitespace.
Yeah – that was my original plan, but it was just as quick to search and replace as I was editing the file anyway.