简体   繁体   中英

PHP read only a part of file

I read/write all content of file in this way

$file = 'c:\file.txt';
$new = 'c:\new.txt';

$current = file_get_contents($file);
file_put_contents($new, $current);

FILE.TXT

some text
some text
etc..
-----START-----
some text
some text
etc..
-----STOP------
some text
some text
etc..

Now I'd like to get only a part of content of a file and write the output in another file. My goal is get and write only this text

-----START-----
some text
some text
etc..
-----STOP------

or alternatively from the line -----START----- (included) until the end of file

How could I do this? thanks

You can use strstr :

$open = '-----START-----';
$close = '-----STOP-----';
$current = file_get_contents($file);
$result = strstr(strstr($current, $open), $close, true). $close;
file_put_contents($conf_file, $result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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