Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to cut blocks of strings from a text file? 1

Status
Not open for further replies.

silkk

Programmer
Jul 14, 2008
16
CZ
Hi guys,

does somebody know some simple script how to cut blocks of strings between matchihg words in a text file?

I have this:

$file = ("path to the file")
$word1 = "HEAD" ;
$word2 = "FOOT" ;

$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
$between=substr($contents, strpos($contents, $word1), strpos($contents, $word2) - strpos($contents, $word1));

echo $between ;

I have in the text more than 10 blocks startin and ending with these words and my task is to cut some of them and to write the result into a new file.

Any suggestions?
Help will be appreciated.

Cheers,
M.
 
Code:
$word1 = "HEAD" ;
$word2 = "FOOT" ; 
$pattern = '/'.$word1.'(.*?)'.$word2.'/imsx';
preg_match_all($pattern, file_get_contents($file), $matches);
echo "<pre>".print_r($matches,true).'</pre>';
 
thanks,jpadie :)
this was helpful :)

cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top