PDA

View Full Version : returns in blogs



deletedUser2352352
July 3rd, 2006, 12:01 PM
I'm making a mini blog.

When a user enters there post in a form and presses return to get space.

How do you get php to see thoses spaces and replicate them on screen.

An example is like in this forum when you post a reply. I'm sure its dead simple and i'm mising something.

bwh2
July 3rd, 2006, 12:03 PM
use nl2br() (http://fr3.php.net/manual/en/function.nl2br.php) or create your own nl2p function.

deletedUser2352352
July 3rd, 2006, 12:07 PM
Cheers ;)

But what if they just press return in the text area? so it looks neat when typed but doesn't separate when called from the database.

deletedUser2352352
July 3rd, 2006, 12:09 PM
ok i see something when you hit return do it place \r into the text in a some sort of hidden form?

bwh2
July 3rd, 2006, 12:17 PM
yes. it places \r\n in, which are replaced by nl2br with a <br />.

Jeff Wheeler
July 3rd, 2006, 12:27 PM
Or, use Matthew Mullenweg's slightly fancier function (http://trac.wordpress.org/browser/trunk/wp-includes/formatting.php?format=txt):


function wpautop($pee, $br = 1) {
$pee = $pee . "\n"; // just to make things a little easier, pad the end
$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
// Space things out a little
$pee = preg_replace('!(<(?:table|thead|tfoot|caption|colgroup|tbody|tr|td| th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquot e|address|math|p|h[1-6])[^>]*>)!', "\n$1", $pee);
$pee = preg_replace('!(</(?:table|thead|tfoot|caption|colgroup|tbody|tr|td| th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquot e|address|math|p|h[1-6])>)!', "$1\n\n", $pee);
$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
$pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
$pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
$pee = preg_replace('!<p>\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td |th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|block quote|address|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
$pee = preg_replace('!<p>\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td |th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|block quote|address|math|p|h[1-6])[^>]*>)!', "$1", $pee);
$pee = preg_replace('!(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td |th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquo te|address|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee);
if ($br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
$pee = preg_replace('!(</?(?:table|thead|tfoot|caption|tbody|tr|td|th|div|d l|dd|dt|ul|ol|li|pre|select|form|blockquote|addres s|math|p|h[1-6])[^>]*>)\s*<br />!', "$1", $pee);
$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)!', '$1', $pee);
$pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') . stripslashes(clean_pre('$2')) . '</pre>' ", $pee);

return $pee;
}

I can't find the smaller version… so, this'll have to suffice. It's a bit more complicated (a lot more, actually).

deletedUser2352352
July 3rd, 2006, 12:33 PM
thats fantastic cheers guys.