note 26037 added to function.nl2br

From: Date: Wed, 16 Oct 2002 01:23:41 +0000
Subject: note 26037 added to function.nl2br
Groups: php.notes 
Request: Send a blank email to php-notes+get-38131@lists.php.net to get a copy of this message
I put together a little function to convert multiple line breaks into XHTML paragraph tags. It also changes newlines within the psuedo-paragraphs to <br />. This should be well suited to CMS where (in my case) you don't want the client to deal with *any* HTML but you still want proper paragraphs for your pages. function autop($pee, $br=1) { $pee = preg_replace("/(\r\n|\n|\r)/", "\n", $pee); // cross-platform newlines $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates $pee = preg_replace('/\n?(.+?)(\n\n|\z)/s', "<p >$1</p >\n", $pee); // make paragraphs, including one at the end if ($br) $pee = preg_replace('|(?<!</p >)\s*\n|', "<br />\n", $pee); // optionally make line breaks return $pee; } NOTE: So the system here wouldn't mangle it I had to add a space inside each of the paragraph tags. Unless you remove these spaces on the forth and fifth line of the function it won't work. USAGE: You can write your text just like I have here, using the enter key as the only formatting tool. This can probably be made more efficient and I'll keep an updated (and unmangled) version of it at http://www.photomatt.net/scripts/autop -- http://www.php.net/manual/en/function.nl2br.php http://master.php.net/manage/user-notes.php?action=edit+26037 http://master.php.net/manage/user-notes.php?action=delete+26037 http://master.php.net/manage/user-notes.php?action=reject+26037

« previous php.notes (#38131) next »