Jump to content

Need help adding paragraph and line break tags to textarea text


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>';
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.