Jump to content

Need help adding paragraph and line break tags to textarea text


tibberous

Recommended Posts

I have a textarea, and am trying to add paragraphs and line breaks to it. Since the paragraphs have a set style in the CSS, I want to use paragraphs for all the main text, and only use line breaks if there are several spaces between a paragraph. Does anyone know how to do this?

Yeah, it is on submit.

 

I got it working for one and two end-lines, but I can't seem to get it to work for three.

 

If my input is something like:

 

Hello


There

 

I was the output to be:

 

<p>Hello</p>

<br>

<p>There</p>

 

Almost like if you types plain text into the WYSIWYG part of Frontpage / Dreamweaver, then went to view code.

 

Here is what I have so far. I replace \r\n with \n, then replace \r with \n. That way I know \n is the only possible linebreak.

 

function format($content){

	$content = trim($content);
	if(strlen($content) == 0) return "";

	$content = '<p>'.$content;
	$content = str_replace("\r\n", "\n", $content);
	$content = str_replace("\r", "\n", $content);		
	$content = preg_replace("/\n{2}/", "</p>\n\n", $content);
	$content = preg_replace("/\n{2}/", "\n\n<p>", $content);
	$content = preg_replace("/[^\n]{1}\n{1}[^\n]{1}/", "<br>\n", $content);

	return $content.'</p>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.