0

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.

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).

I am planning to use fopen, fread (read only) and fclose to read the file contents. I have experience of using these functions already.

I am looking to read all the contents in a selected part of the file. i.e.

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.

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.

Community
  • 1
  • 1
Justice
  • 169
  • 1
  • 2
  • 9
  • Have you made an attempt to solve this? What specific problem with `fread()` are you having? Can you show your code and explain exactly what does not work as expected? – Mike Brant Jan 23 '14 at 00:03
  • My issue is actually being able to use `fread()` with those tags to select the text BETWEEN the tags. I can get the whole file contents (well... if it wasn't too large...). I just have no clue (and can't find any references on the internet) of how to implement the selection WHILE reading (rather than after reading the file into memory). - this is what I'm having issues with. – Justice Jan 23 '14 at 00:07

1 Answers1

0

You could grep the text file which would only return the text with a matching tag.

 $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. 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. Regards,

rwhite
  • 430
  • 2
  • 11
  • I can get this working until the `$result = exec('grep -in "'.$searchstr.'" '.$fpath);` line - this just returns a blank value all the time. (when the tag exists identically in my script and the text file and the text file is being referenced correctly) – Justice Jan 23 '14 at 11:53
  • Have a read through these comments(http://us2.php.net/manual/en/function.exec.php), depending on your system, you may have to double quote the the whole command if on a Windows system. $result without any additional processing should look like `2:<>TEXT TEXT TEXT<>` where 2: is the line number followed by the text. exec( ) also has an exit status you can test. – rwhite Jan 23 '14 at 14:56
  • Should there be comma's in the `exec()` statement? I've never used this and don't really understand the "grep" part to be honest. Do I need some form of plugin on my server to use this? – Justice Jan 23 '14 at 15:06
  • No you shouldn't need a comman. _grep_ is a command line program. _exec()_ is simply executing a unix program and depending on your setup, there may be nuances to using grep. _grep_ is standard with most OS. From the command line run `man grep` to read grep's manual. – rwhite Jan 23 '14 at 15:18
  • If by command line you mean windows command prompt - typing `man grep` gives the message that it is not recognised as a command (if I have understood you correctly?) – Justice Jan 23 '14 at 15:35
  • Ah, therein lies the problem. Windows may not have grep. From the command prompt I did a quick search on a Windows machine with `findstr grep` and didn't return any results. Seems grep is not an option for you. – rwhite Jan 23 '14 at 16:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45929/discussion-between-rwhite35-and-alex-hogg) – rwhite Jan 23 '14 at 16:18
  • That seems unfortunate then, but would explain why it didn't work on my machine. Are there any alternatives to `grep` that you know of which I could try using? Thanks for all of your time regardless of the outcome! :) – Justice Jan 23 '14 at 16:19