faqts : Computers : Programming : Languages : PHP : Common Problems : Files

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 5 people (100%) answered Yes
Recently 4 of 4 people (100%) answered Yes

Entry

How do I fread only the lines of a file that exist between two markers (like %%data_start%% - %%data_end%%) in a file?

Jan 13th, 2002 16:57
Jens Clasen, Peter Torkelson,


Once uppon a time I wrote a little snippet for that:
<?php
# NOTE:
# -----
# o This Function does not include any Error-Checking!
# o This Function works only upto a limited filesize!
#
#  This Function has been released at 
#  http://php3-forum.de/forum.php3?nr=14811
#  as well.
function split_file($file,$starttag,$endtag){
  # Read File
  $f=file($file);
  $content=implode('\n',$f); # make a string out of it
  $content=spliti("($starttag|$endtag)",$content); # Split by Start & End
  for($i=0;($i+1)<count($content);$i+=2) # every second entry has to be
                                         # a result now!    
    $line[]=explode("\n",$content[$i+1]);
  return $line;
}
?>
If Your File for example looks like that:
  ---------------Test.html-----------------
  <HTML>
  <HEAD>
  <TITLE> Test Test Test</TITLE>
  </HEAD>
  <BODY>
  </BODY>
  </HTML>
  -----------------------------------------
and if You just want the Head-Section out of it, You can use:
  $heads=split_file("Test.html","<HEAD>","</HEAD>");
  print_r($heads[0]);
and the Head-Section will be displayed!
P.S.: As always: Please excuse my bad English - it's not my native 
                 Language