Jump to content

Delete lines in file ( Between given 2 strings )


mart94

Recommended Posts

Hi!

 

What i can't figure out, is how to delete between 2 given strings in another file.

 

Explanation:

I have a file, it contains following:

### Start of content 0 ###
Some
Radom
Lines with text 1
### End of content 0 ###

### Start of content 1 ###
Some
Radom
Lines with text 2
### End of content 1 ###

### Start of content 2 ###
Some
Radom
Lines with text 3
### End of content 2 ###

And so on, now i need code, which can find line i give and delete between.

 

Something like this:

 

If, i give string:

### Start of content 0 ###

 

Then it will delete this line to the

 

### End of content 0 ###

 

Hope you can help!

 

All the best:

Mart L.

Link to comment
Share on other sites

function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}

$fullstring = "this is my [tag]dog[/tag]";
$parsed = get_string_between($fullstring, "[tag]", "[/tag]");

echo $parsed; // (result = dog)

 

something along the lines of this may help you

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.