Forum Moderators: coopster

Message Too Old, No Replies

Preserve Paragraph Breaks

Not line breaks

         

ukgimp

11:08 am on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Only just noticed this but:

$content = nl2br($content);
$print "<p>$content</p>";

Produces this:

<p>line 1<br />
line 2<br />
<br />
line 3</p>

Can it be done with somethind so the following occurs:

<p>line 1</p>
<p>line 2</p>
<p>line 3</p>

Cheers

Warboss Alex

12:22 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



<?php

$text = 'Some text..<br /> with line breaks..';
$text = nl2br($text);
$text = str_replace( '<br />', '</p><p>', $text);
echo '<p>'.$text.'</p>';

?>

Should do it. :)

hakre

9:06 pm on Apr 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well, that should do it, but why to use nl2br any longer?:


$content = str_replace("\n","</p>\n<p>",$content);
$print "<p>$content</p>";

That should do the trick properly. Don't feel offended ;)

--hakre

WhosAWhata

9:53 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



its actually a fairly common function to make

function nl2p($content) {
$content = explode("\n",$content);
foreach($content as $key => $line) {
$lines[$key] = "<p>".$line."</p>";
}
$content = implode("\n",$lines);
return $content;
}

should do the trick

Netizen

10:02 pm on Apr 27, 2004 (gmt 0)

10+ Year Member



I would do

$content=sprintf('<p>%s</p>',preg_replace("/\r*\n/","</p>\n<p>",$content));

(Untested code, as usual)

ergophobe

3:28 pm on Apr 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can I just ask - do your linebreaks (or newlines, or whatever) really correspond to paragraphs? I mean, from an HTML standpoint, is that what you really want?