php line break insertion

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
i've just realised that i've got an issue when people are posting comments on my site, user line breaks aren't showing so the comments are looking pretty jumbled. what do i need to do to convert a user entered line break (when they press return) into an html <br />? i'm guessing it's str_replace(), but what i was trying hasn't worked.

i've been trying str_replace("\n","<br />","$body"), but it's not working

can anyone shed some light?

thanks :)
 
No, use autop:

http://photomatt.net/scripts/autop

Code:
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
	$allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|style|script|object|input|param|p|h[1-6])';
	$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
	$pee = preg_replace('!(</' . $allblocks . '>)!', "$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>(<div[^>]*>\s*)|', "$1<p>", $pee );
	$pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
	$pee = preg_replace( '|<p>|', "$1<p>", $pee );
	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\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*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
	if ($br) {
		$pee = preg_replace('/<(script|style).*?<\/\\1>/se', 'str_replace("\n", "<WPPreserveNewline />", "\\0")', $pee);
		$pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
		$pee = str_replace('<WPPreserveNewline />', "\n", $pee);
	}
	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
	$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
	if ( strstr( $pee, '<pre' ) )
		$pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') .  stripslashes(clean_pre('$2'))  . '</pre>' ", $pee);
	$pee = preg_replace( "|\n</p>$|", '</p>', $pee );
/**/
	return $pee;
}

BRs aren't semantically correct, Ps are :)
 
Code:
$string = '<p>' . str_replace("\n", "</p>\n<p>", $string) . '</p>';
:o

can also use wordwrap() in there, should you be such inclined.

Semantics dictate <p>'s are for paragraph's, whilst <br />'s are for line breaks, btw. However, due to the lack of closing tag it's impossible to use them effectively with styles.
 
i want to use something like Dj_Jestar's, but it's not working. if i pass the following through a text field:

test
test
test

it comes out as

testtesttest

what could this be? i'm wasting far too much time on this when it's probably really simple!

i've tried \r\n, \n, \r and \n\n so far and none of those have been replaced with what i want. i'd be really grateful for some help :)
 
Back
Top Bottom