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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

\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>');
}
?>

Link to comment
Share on other sites

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.