Wrapping text in P tag

I am trying to figure out how to wrap text like this:

Morbi nisl dead, high priest laoret eu, lobortis id ipsum. Integer skelery, blandite, pulvinar. Nam tempus mi eget nunc laoreet venenatis. Proin Viverra, Erat in Accumsan Tincidunt, Ante Mi Cursus Elit, not

congue mauris dolor ac elit. Maecenas Mollis NISL Sem Semper Ornar. Integer nunc purus, dapibus nec dignissim sed, dictum eget leo. Etiam in mi ut erat pretium fringilla sed

In it:

<p> Morbi Nisl Mistor, "Summary," Laoret EU, "Lobortis and Ipsum." Integer skelery, blandite, pulvinar. Nam tempus mi eget nunc laoreet venenatis. Proin Viverra, Erat at Accumsan Tincidunt, Ante Mi Cursus Elit, not </p>

<p> congue mauris dolor ac elit. Maecenas Mollis NISL Sem Semper Ornar. Integer nunc purus, dapibus nec dignissim sed, dictum eget leo. This in my morning look. </P>

Pay attention to the p tags around the text.

+9
php regex tags wrap
source share
3 answers

It should do it

 $text = <<<TEXT Morbi nisl tortor, consectetur vitae laoreet eu, lobortis id ipsum. Integer scelerisque blandit pulvinar. Nam tempus mi eget nunc laoreet venenatis. Proin viverra, erat at accumsan tincidunt, ante mi cursus elit, non congue mauris dolor ac elit. Maecenas mollis nisl a sem semper ornare. Integer nunc purus, dapibus nec dignissim sed, dictum eget leo. Etiam in mi ut erat pretium fringilla sed TEXT; $paragraphedText = "<p>" . implode( "</p>\n\n<p>", preg_split( '/\n(?:\s*\n)+/', $text ) ) . "</p>"; 
+10
source share

Use preg_replace in a loop over all lines of your input:

 $replacement = preg_replace("/(.*)/", "<p>$1</p>", $current_line); 
+3
source share
 $str = '<p>'. str_replace('\n\n', '</p><p>', $str) .'</p>'; 

OR

 $str = '<p>'. preg_replace('\n{2,}', '</p><p>', $str) .'</p>'; 

To catch 2 or more.

+2
source share

All Articles