从 PHP 读取文本文件时如何指定特定的分隔符?

发布于 2024-09-09 06:12:45 字数 871 浏览 2 评论 0原文

我有一个文本文件,我被告知分隔符如下引用。我的问题是如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2024-09-16 06:12:45

PHP 中还有 chr() 函数。因此,您应该能够使用与 PERL 中相同的语法:

$field_separator = chr(1); 
$record_separator = chr(2) . "\n";

您尝试过吗,或者您还有其他问题吗?或者你想用 PHP 创建一个类似的文件吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文