Automatically add <p> tags to user input.

  • natas
  • PHP Ninja
  • Proficient
  • No Avatar
  • Joined: Mar 28, 2009
  • Posts: 305
  • Loc: AFK
  • Status: Offline

Post July 15th, 2011, 2:27 am

I've created an input form for a friend of mine.

I need to figure out a way to automatically add <p> tags to his input. Even though he enters the text as paragraphs, it gets entered into the database without any formatting. I hope I'm not being too vague.

Any ideas on how to do this?
Custom Web Design
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 15th, 2011, 2:27 am

  • this213
  • Guru
  • Guru
  • User avatar
  • Joined: Mar 01, 2004
  • Posts: 1242
  • Loc: ./
  • Status: Offline

Post July 15th, 2011, 3:51 am

Just concatenate it together with .
PHP Code: [ Select ]
$formatted = '<p>'.nl2br($input).'</p>'

nl2br will convert line breaks to html <br> tags too.
http://www.disabo.com
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Joined: Apr 28, 2011
  • Posts: 214
  • Loc: Sweden
  • Status: Offline

Post July 15th, 2011, 4:04 am

Not being able to add it despite entering <p>text</p> suggests that the input maybe is "cleaned"/sanitized and that there may be some text altering functions added before going to the database such as "striptags()".
  • this213
  • Guru
  • Guru
  • User avatar
  • Joined: Mar 01, 2004
  • Posts: 1242
  • Loc: ./
  • Status: Offline

Post July 15th, 2011, 4:11 am

This is true, I should have pointed out that this should be after pulling that data out of the database when you're prepping it for output. I didn't even notice the OP said it was going into the db without any formatting (which is exactly how it should be).
http://www.disabo.com
  • natas
  • PHP Ninja
  • Proficient
  • No Avatar
  • Joined: Mar 28, 2009
  • Posts: 305
  • Loc: AFK
  • Status: Offline

Post July 15th, 2011, 9:25 am

I may have been to vague. I can indeed add the <p> tags. My problem is that there is more than one paragraph most of the time. How can I detect news paragraphs and add the tags accordingly?
Custom Web Design
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Joined: Apr 28, 2011
  • Posts: 214
  • Loc: Sweden
  • Status: Offline

Post July 15th, 2011, 4:14 pm

Posting some code (or just "typical" user input that need <p> tags, and where) could be helpful.
  • this213
  • Guru
  • Guru
  • User avatar
  • Joined: Mar 01, 2004
  • Posts: 1242
  • Loc: ./
  • Status: Offline

Post July 16th, 2011, 4:45 pm

Just put everything you can think of that would define a paragraph into an array, then pass that to str_replace as such:
PHP Code: [ Select ]
trim($data);
$data = str_replace($paragraph_markers,'</p><p>',$data);
  1. trim($data);
  2. $data = str_replace($paragraph_markers,'</p><p>',$data);
http://www.disabo.com
  • natas
  • PHP Ninja
  • Proficient
  • No Avatar
  • Joined: Mar 28, 2009
  • Posts: 305
  • Loc: AFK
  • Status: Offline

Post July 17th, 2011, 12:18 am

this213 wrote:
Just put everything you can think of that would define a paragraph into an array, then pass that to str_replace as such:
PHP Code: [ Select ]
trim($data);
$data = str_replace($paragraph_markers,'</p><p>',$data);
  1. trim($data);
  2. $data = str_replace($paragraph_markers,'</p><p>',$data);


I'm not sure I follow. The only thing that would indicate a new paragraph would be the user hitting the "return" key.
Custom Web Design
  • this213
  • Guru
  • Guru
  • User avatar
  • Joined: Mar 01, 2004
  • Posts: 1242
  • Loc: ./
  • Status: Offline

Post July 17th, 2011, 10:03 am

Then that's what you're going to have to match on. Something like:
PHP Code: [ Select ]
$paragraph_markers = array("\n\n","\n");
http://www.disabo.com
  • natas
  • PHP Ninja
  • Proficient
  • No Avatar
  • Joined: Mar 28, 2009
  • Posts: 305
  • Loc: AFK
  • Status: Offline

Post July 17th, 2011, 10:50 pm

So the user will have to input the "/n" when he enters the data?
Custom Web Design
  • this213
  • Guru
  • Guru
  • User avatar
  • Joined: Mar 01, 2004
  • Posts: 1242
  • Loc: ./
  • Status: Offline

Post July 17th, 2011, 11:12 pm

"\n" is what gets stored every time the user hits the enter key.
http://www.disabo.com
  • natas
  • PHP Ninja
  • Proficient
  • No Avatar
  • Joined: Mar 28, 2009
  • Posts: 305
  • Loc: AFK
  • Status: Offline

Post July 18th, 2011, 10:33 am

this213 wrote:
"\n" is what gets stored every time the user hits the enter key.


Heh... I must have overlooked that. Ugh. Thank you very much!
Custom Web Design

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 166 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.