note 26037 modified in function.nl2br by aidan

From: Date: Sun, 10 Oct 2004 10:38:52 +0000
Subject: note 26037 modified in function.nl2br by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-78281@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. 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 --was-- 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://php.net/manual/en/function.nl2br.php

« previous php.notes (#78281) next »