As discussed above, beware that using regex to parse HTML, especially for "complex" problems, is generally a bad idea. The following is not a perfect solution, but may be good enough to the simple requirements you've given above:
/(?<=<div>).*?(?=<\/div>)|(?<=<br>\s*<br>).*?(?=<div>|<br>\s*<br>)/is
The (?<=...)
and (?=...)
are look behinds/aheads, i.e. they assert that those sections of the pattern are present, but are not included in the match result.
I have also used \s*
to help catch scenarios where the user types something like:
<br> <br>
Or:
<br>
<br>
...But as I say, this is still not a perfect solution. If you find the pattern gets too complex, then seriously consider using an XML parser instead. (Or, how about just letting the user enter new lines, and converting these into paragraphs for them? ... Or even, just use an existing WYSIHTML5 library, or a markdown library?)
...` as paragraph. My current code is this: `preg_match("/
's are detected. – IMarks Feb 03 '17 at 10:41
...
`? And what about ` `? What if someone uses ``? Do you qualify lists (e.g. `
`) as "paragraphs"? How do you treat titles/subtitles, like ``s, potentially combined with any of the above? Think carefully about your requirements, and re-consider [this post](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454).
– Tom Lord Feb 03 '17 at 10:49...
`, `...
`, `...
`, etc? What about **nested** `and – IMarks Feb 03 '17 at 10:56