如何根据PHP中的一行拆分文件?

发布于 2025-01-23 13:17:31 字数 925 浏览 3 评论 0原文

我有一个名为doremi.txt的文件,如下所示,

These are the notes 
I sing
They fly and dance 
like a bird's wing
--------------------
Do
Re
Mi
Fa
So
La
Ti
TaDa!

我想使用此文件阅读文本文件,但我只想要在虚线之后的信息(音符注释):

// filename in this case is DoReMi.txt

        $file = fopen ( $filename, 'r' );
        $fileContents = nl2br ( fread ( $file, filesize( $filename )));

        $lines = 0;
        while(!feof($file)){
            $curr = fgets($file, 999);
            $lines++;
            // this should count the number of lines in the text file
        }

        echo "--------------------";
        // This is my problem area
        // I want to "ignore" the information before this line


        fclose( $file );
        echo "# of notes: $lines";
        echo "<hr/> $fileContents <hr/>";

因为这是一个文件,而不是一个文件字符串或列表,我知道我无法创建子字符串或新数组。

此外,FGETSCSV无法使用,因为它是单行而不是文件中的每一行。

I have a file named doremi.txt, which goes as follows

These are the notes 
I sing
They fly and dance 
like a bird's wing
--------------------
Do
Re
Mi
Fa
So
La
Ti
TaDa!

I want to read through the text file using this, but I only want the information that is after the dashed line (the musical notes):

// filename in this case is DoReMi.txt

        $file = fopen ( $filename, 'r' );
        $fileContents = nl2br ( fread ( $file, filesize( $filename )));

        $lines = 0;
        while(!feof($file)){
            $curr = fgets($file, 999);
            $lines++;
            // this should count the number of lines in the text file
        }

        echo "--------------------";
        // This is my problem area
        // I want to "ignore" the information before this line


        fclose( $file );
        echo "# of notes: $lines";
        echo "<hr/> $fileContents <hr/>";

Since this is a file and not a string or a list, I know that I cannot create a substring or new array.

In addition, fgetscsv wouldn't work because it is a single line rather than each line in the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文