简体   繁体   English

从大文本文件(长度可变的文本)中读取选定的内容

[英]Read SELECTED contents from a large text file (varying length text)

I'm looking to read contents of a file between two tags in a large text file (so can't read the whole file at once due to memory restrictions on my server provider). 我希望读取大文本文件中两个标签之间的文件内容(由于服务器提供程序的内存限制,因此无法一次读取整个文件)。 This file has around 500000 lines of text. 该文件包含约500万行文本。

This ( PHP: Read Specific Line From File ) isn't an option (I don't think), as the text I need to read varies in length and will take up multiple lines (varies from 20-5000 lines). 这( PHP:从文件读取特定行 )不是一种选择(我不认为),因为我需要读取的文本长度不同,并且会占用多行(从20-5000行不等)。

I am planning to use fopen , fread (read only) and fclose to read the file contents. 我打算使用fopenfread (只读)和fclose读取文件内容。 I have experience of using these functions already. 我已经有使用这些功能的经验。

I am looking to read all the contents in a selected part of the file. 我希望读取文件选定部分中的所有内容。 ie

File contents example 文件内容示例

    <<TAGNAME-1>>AAAA AAAA AAAA<<//TAGNAME-1>>
    <<TAGNAME-2>>TEXT TEXT TEXT<<//TAGNAME-2>>

To select the text "AAAA AAAA AAAA" between the <<TAGNAME-1>> and <<//TAGNAME-1>> when TAGNAME-1 is called as a variable in my script. 当在我的脚本TAGNAME-1称为变量时,要在<<TAGNAME-1>><<//TAGNAME-1>>之间选择文本"AAAA AAAA AAAA"

How could I go about selecting all the text between the two tags that I require? 如何选择需要的两个标签之间的所有文本? (and ignore the remainder of the file) I have the ability to create the two tags where required in my php script - my issue is implementing this within the fread function. (并且忽略文件的其余部分)我有能力在php脚本中所需的位置创建两个标记-我的问题是在fread函数中实现此标记。

You could grep the text file which would only return the text with a matching tag. 您可以grep文本文件,该文件仅返回带有匹配标签的文本。

 $tagnum = 2;     //variable 
 $pattern = "<<TAGNAME-";   
 $searchstr = $pattern.$tagnum;   //concat the prefix with the tag number
 $fpath ="testtext.txt";  //define path to text file
 $result = exec('grep -in "'.$searchstr.'" '.$fpath);
 echo $result;

Where $tagnum would define each tag to search. $ tagnum将定义每个要搜索的标签。 I've tested it in my sandbox and it works as expected. 我已经在沙盒中对其进行了测试,并且可以正常工作。 Note this will read the whole line until the end tad or newline is reached. 请注意,这将读取整行,直到到达末尾tad或换行符为止。 Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM