Writing to a file with start and end

Posted 11 months ago by dk4210

Hello guys,

This i mainly a PHP question.

I have a file that contains two comments

#start

#end

I would like to search in the file and find the #start and #end tags and replace whatever is between it with custom content.

Here's the code I have so far. This will only replace one line though, which is the #start line only. I need it to replace between the two comments.

$filename = (public_path('/../tests/Browser/mytest.txt'));

    $find = "#start";
    $replace = "whatever";
    $file = $filename;
    $contents = file_get_contents($file);
    $contents = str_replace($find,$replace,$contents);
    file_put_contents($file,$contents);

Please advise

Thanks!

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.