Suppose there is a line where the text is mixed up as there is no text enclosed in p tags:

раз два <p>три <a href='#'>четыре</a> пять</p> <img src="#">шесть <strong>семь</strong> <p>восемь</p> девять десять 

Can anyone have an example of how all the text is not enclosed in p, to conclude in it:

 <p>раз два</p> <p>три <a href='#'>четыре</a> пять</p> <p><img src="#">шесть <strong>семь</strong></p> <p>восемь</p> <p>девять десять</p> 

The text is organized without line feeds.

I tried the example https://stackoverflow.com/questions/5961217/how-do-i-surround-all-text-pieces-with-paragraph-tags?answertab=active#tab-top (nl2p_html) but it eats all the attached tags .

  • " The text is organized without newline characters " - first, you have it wrong, there are newlines . Secondly, by what criteria are then supposed to allocate fragments for the conclusion in paragraphs? - PinkTux
  • Examples with the translation of the line I gave for clarity. And the criterion is the absence of the p tag (I thought maybe someone has a tricky regular season). - posix
  • What does "no p tag" mean? In the whole original text of "War and Peace" there is not a single p tag, is this, it turns out, one big paragraph? Or do you need to insert it through each word? - PinkTux
  • Yes, in my case - this is one big paragraph, and if in the text before or after there was this tag, then the tag should be shoved with a specific piece of text (along with other tags that may be in it). - posix

1 answer 1

Well, something like this:

 $pattern = '/(^|<\\/[^>]*p>)(.*)(<[^>]*p>|$)/isU'; $replacement = '$1<p>$2</p>$3'; $subject = 'ваша строка'; echo preg_replace($pattern, $replacement, $subject, -1 ); 
  • Thank you for what you need. - posix