Jump to content

[SOLVED] Getting a <P>aragraph without the <p> into MySQL?


suttercain

Recommended Posts

Hi Guys,

 

Does anyone know a technique or code that allows a user entering text into a <textarea> to enter paragraphs without having the user enter html or the <p> tag that will be stored in MySQL then displayed to the browser.

 

I currently use TinyMCE to do this but would like a simpler option. I don't mind if the code in the MySQL database hase the <p> tag and closing </p> tag form XHTML but I would like it to automatically be genrated if the user hits enter to space the paragraphs.

 

EXAMPLE:

How text is displayed from MySQL if the user enter spaces, they are not reflected:

Taylor, along with Vick and two other co-defendants, had pleaded not guilty Thursday before U.S. District Judge Henry E. Hudson. The trial for all four defendants is scheduled for November 26.

 

This is how I want it to look:

Taylor, along with Vick and two other co-defendants, had pleaded not guilty Thursday before U.S. District Judge Henry E. Hudson.

 

The trial for all four defendants is scheduled for November 26.

 

 

So is there a way to get Paragraph breaks with the user just hitting enter and some how recognize that enter as a <p> tag?

 

Thanks

Perhaps you need to look into the nl2br() function before adding it to the database.

 

alternatively something like this could be done.

$text = str_replace("\n","</p><p>",$text);

$text .= "</p>";

 

Your problem is the textarea considers hitting the enter key as a newline character or \n

Hi Lightning Strike,

 

I thought with nl2br you still have to enter /n in the code. I want the user to only have to hit enter and now html code to reflect a paragraph break.

 

Correct me if I am wrong.

 

 

Thanks

Okay so I have tried a few of the nl2br functions, like this one

 

<?php
function nls2p($str)
{
  return str_replace('<p></p>', '', '<p>'
        . preg_replace('#([\r\n]\s*?[\r\n]){2,}#', '</p>$0<p>', $str)
        . '</p>');
}
?>

 

Then I entered this into the textarea:

 

Hello. Test1

 

Test2

 

 

 

test3

 

 

 

When the above was echoed from MySQL it came out like this:

Hello. Test1 Test2 test3

 

Also when I looked at the inout in the MySQL field it has no \n or any other tags <br /> <p>.. nada.

 

 

What am I doing wrong?

 

Thanks.

\n and \r characters are there but they are invisible.

 

Also you should use double quotes when you are using escape characters:

<?php
function nls2p($str)
{
  return str_replace('<p></p>', '', '<p>'
        . preg_replace("#([\r\n]\s*?[\r\n]){2,}#", '</p>$0<p>', $str)
        . '</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.