<?php $for_edit = "подробнее"; // искомая строка 1 $for_edit2 = "подробнее2"; // искомая строка 2 $what = "кратко"; // на эту меняем $fopen = @file("21.txt"); foreach ($fopen as $key => $value) { if (substr_count($value, $for_edit)) { if (isset($fopen[$key + 1])) { array_splice($fopen, $key + 1, 1, $what); } } $f = fopen("21.txt", "w"); for ($i = 0; $i < count($fopen); $i++) { fwrite($f, $fopen[$i]); } fclose($f); } ?> 

The script searches for a specific string and changes the next one to the right one, how to make the script search 2 specific lines and everything between them, change it to the necessary string? (Even it is impossible to format the text.)

  • and try to regular season fails? - KoVadim

3 answers 3

 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, guess? =)

  • If possible, write how. I just started learning php. - shol September
  • one
    [str_replace] [1] 'om. [1]: php.net/manual/en/function.str-replace.php - ling
  • Is it possible in the finished form? I'll understand better then. - shol September
  • Um ... You have two lines between which you are looking for: $s1 and $s2 , the line in which you are looking for: $str and replacement: $rpc . The code will look like this: $ q = getBetween ($ s1, $ s2, $ str); $ str = str_replace ($ s1. $ q. $ s2, $ s1. $ rpc. $ s2, $ str); But this is all taking into account that you actually have $s1 and $s2 in $str . - ling

as an option, a function that finds and replaces all matches between the required 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')); 

    As an option (I will not write the whole script, I think you will understand):

     $text; // содержимое нашего файла $str1; // Строка 1 $str2; // Строка 2 $to_r; // Текст на который будет меняться информация между строкой 1 и 2 preg_replace('#('.$str1.').*('.$str2.')#msi', '$1'.$to_r.'$2', $text); 
    • Regular work is a good thing, but dangerous. Try replacing "x" in the string "/ asd;) x (" for something. I mean the text between ")" and "(" - DL_
    • Just try it ;-) $ text = "/ asd;) x ("; $ text = preg_replace ("# (\ / asd \;)) x (() #", '$ 1text $ 2', $ text); echo $ text; Actually, I’ve never seen anything more flexible than regular expressions for word processing. Besides, I often work with a lot of information and regular expressions provide a very good speed advantage ... - Daniel Wendolin
    • returns the void .. so it should be? and following your example should look like this: preg_replace ('# ()). * (() # msi', '$ 1text $ 2', $ text); - DL_
    • I apologize. The parser ate the text: $ text = preg_replace ("# (\\ / asd \;)) x (() #", '$ 1text $ 2', $ text); - Daniel Vendolin September
    • Here ... The controlling symbols have already begun to be screened, a regular operating schedule will be just a bit longer;) Regulars are a very good and flexible tool, but, I repeat, it must be used very carefully. - DL_