I need to take from the text file only the part where there are markers between them, for example, there is text, I need to take only the part that is between <<pop and <<pop .

    3 answers 3

    This requires regular expressions. But you can try, like this:

     echo "Ваш идентификатор: ".session_id()."</br>"; $mystring = "12345 рвораыв <<pop Нужный текст1 <<pop 657890 Текст какой-то <<pop Нужный текст2 <<pop"; $content = split("<<pop", $mystring); // "первый параметр - разделитель", второй - сама строка // возвращает массив echo $content[1]; echo $content[3]; 
    • split it is DEPRICATED, so it is better to use explode - chernomyrdin

    It is possible through substr :

     $input = "xxx <<popSomeText<<popcorn"; $marker = "<<pop"; $pos1 = strpos($input, $marker); if ($pos1 !== FALSE) { $pos1 += strlen($marker); $pos2 = strpos($input, $marker, $pos1); if ($pos2 !== FALSE) { $output = substr($input, $pos1, $pos2 - $pos1); } else { die("не найден маркер конца"); } } else { die("Не найден маркер начала"); } echo $output, "\n"; 

    It is possible through preg_match :

     $input = "xxx <<popSomeText<<popcorn"; $marker = "<<pop"; if (preg_match("/\Q$marker\E(.*)\Q$marker\E/",$input,$match)) { $output = $match[1]; } else { die("маркеры начала/конца не найдены"); } echo $output, "\n"; 

    But here you need to understand that (.*) And (.*?) Will work differently, you can learn more about regular expressions

      https://ru.stackoverflow.com/questions/36729/replacement-line-in-file