Hello. There is a line containing the text of the type:

<p>заголовок</p> <p>содержание</p> <p>еще что-то</p>

Q: How do I cut only the content ? it always comes with the second <p></p>

That is, I need to make a line like this from this line:

<p>заголовок</p> <p>еще что-то</p>

I would be very grateful for the help.

  • Text and tag unchanged, always <p>содержание</p> ? - Crantisz
  • tag is always the same. Text is always different in this tag - iKey

3 answers 3

 preg_replace('/^.*?<\/p>.*?\K<p>.*?<\/p>/is','',$str); 

Expression Expression:

 ^ # Начало строки .*? # любые символы <\/p> # конец первого тега P .*? # любые символы \K # точка начала совпадения (замены) <p>.*?<\/p> # вырезаемые символы в теге /is # флаги выражения, без учета регистра, однострочное (. включает перевод каретки) 

    Option with DOMDocument :

     $dom = new DOMDocument; $dom->loadHTML($yourStr); $p = $dom->getElementsByTagName('p')->item(1); var_dump($p->textContent); 

      You can try using regular expressions.

       $output=preg_replace("/<\/p>[^<]*<p>[^>]*<\/p>/", "</p>", $input_lines); 

      Online test: http://www.phpliveregex.com/p/jkS

      • The fact is that in the second tag <p></p> the same text is not always the same. rather, it is always different there. the only thing that is permanent is that this text is wrapped in a <p> and the fact that this tag always comes second, for example: <p>блабла</p> <p>это вырезать</p> <p>текст какой-то</p> -
      • @ Denis Got it, edited the answer - Crantisz