Replacing lines in a file

The script searches for a specific line and changes the next one to the desired one, how can I make the script search for 2 specific lines and change everything between them to the desired line? (Even I can’t format the text.)


Answer 1, authority 100%

function getBetween($s1, $s2, $str)
{
    $s = strpos($str, $s1) + strlen($s1);
    $e = strpos($str, $s2);
    return substr($str, $s, $e-$s);
}
echo getBetween('<img ', ' />', '<img src="lalala.gif" alt="lalala song" />');

How to replace, I think you can guess? =)


Answer 2

as an option, a function that finds and replaces all matches between the searched strings:

function replaceBetween($s1,$put,$s2,$str){
$s = explode($s1,$str);
foreach($s as $k=>$r){
$n=strpos($r,$s2);
$out .= ($n?$s1.$put:($k?$s1:'')).substr($r,$n);
}
return $out;
}
//   :
echo replaceBetween($for_edit,$what,$for_edit2,file_get_contents('21.txt'));

Answer 3

As an option (I won’t write the whole script, I think you’ll figure it out):

$text; //   
$str1; //  1
$str2; //  2
$to_r; //         1  2
preg_replace('#('.$str1.').*('.$str2.')#msi', '$1'.$to_r.'$2', $text);