从 PHP 读取文本文件时如何指定特定的分隔符?
我有一个文本文件,我被告知分隔符如下引用。我的问题是如何在 PHP 中设置分隔符?
即这将在我的脚本中将其设置为制表符分隔符字段
$csv->delimiter = "\t"; # tab delimited
分隔符(FS):SOH(ASCII 字符 1)
记录分隔符(RS):STX(ASCII 字符 2)+“\n”
例如,这些字符以编程语言表示如下所示:
Perl:
$field_separator = chr(1);
$record_separator = chr(2) . "\n";
Java:
String fieldSeparator = String.valueOf((char)1);
String recordSeparator = String.valueOf((char)2) + "\n";
文件示例如下所示:
#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
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PHP 中还有
chr()
函数。因此,您应该能够使用与 PERL 中相同的语法:您尝试过吗,或者您还有其他问题吗?或者你想用 PHP 创建一个类似的文件吗?
There is also the
chr()
function within PHP. So you should be able to use the same syntax as in PERL:Have you tried it out, or do you have another issue? Or do you want to create a file like that in PHP?