How to specify a specific delimiter when reading a text file from PHP?
I have a text file, I am told the delimiter is as quoted below. My question is how to I set the delimiter inside PHP?
ie this would开发者_如何学Python set it as TAB delimiter in my script
$csv->delimiter = "\t"; # tab delimited
Field Separator (FS): SOH (ASCII character 1)
Record Separator (RS) : STX (ASCII character 2) + "\n"
For example, these characters are represented in programming languages as shown below:
Perl:
$field_separator = chr(1);
$record_separator = chr(2) . "\n";
Java:
String fieldSeparator = String.valueOf((char)1);
String recordSeparator = String.valueOf((char)2) + "\n";
A sample of the file looks like this:
#export_datecollection_type_idname
#primaryKey:collection_type_id
#dbTypes:BIGINTINTEGERVARCHAR(200)
#exportMode:FULL
12766788028576TV Season
12766788028578Movie Bundle
12766788028577Ringtone
12766788028574Compilation
12766788028571Album
12766788028572MaxiSingle
12766788028573Orphan
12766788028575Audiobook
#recordsWritten:8
There is also the chr()
function within PHP. So you should be able to use the same syntax as in PERL:
$field_separator = chr(1);
$record_separator = chr(2) . "\n";
Have you tried it out, or do you have another issue? Or do you want to create a file like that in PHP?
精彩评论