tags:

views:

350

answers:

3
+1  Q: 

Wrap Text in P tag

Hi, I'm trying to figure out how to wrap text like this :

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

into this :

<p>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</p>

<p>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</p>

Note the p tags around the text.

+2  A: 
$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.

Matthew Vines
Typo: $str = '<p>'. str_replace($str, '\n\n', '</p><p>') .'</p>';
simplemotives
Thank you sir, fixing, i forgot the order of parameters too.
Matthew Vines
When I'm using similar to this, I'll often do \n\n+ or \n{2,} to allow two or more newlines, rather than requiring exactly two newlines.
Peter Boughton
(If doing above, the function would need to be replaced with a regex one, not a regular string replace.)
Peter Boughton
Then I would do something similar, only using preg_replace and regex to get 2+ \n characters. It all depends on how well defined the structure of your input is.
Matthew Vines
I can't get this one to work on my text, see here : http://codepad.org/M5mRl8jE . I need it to wrap each bit of text. Thanks
The Pixel Developer
str_replace is going to be too "dumb" to make the correct match for you. Check my answer below.
Peter Bailey
+1  A: 

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

$replacement = preg_replace("/(.*)/", "<p>$1</p>", $current_line);
Jeremy Smyth
This will work since - by default - the . excludes newlines. Of course, it does assume that there is no 'hard wrapping' occuring within the content.
Peter Boughton
+2  A: 

This 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>";
Peter Bailey
Thanks, this worked great. You did have a bug though, see my code here. http://codepad.org/soU8q5lL
The Pixel Developer
Ah, yes, I see. Fixed it, thx!
Peter Bailey