Automatically add <p> tags to user input.

  • natas
  • PHP Ninja
  • Proficient
  • natas
  • Posts: 308
  • Loc: AFK

Post 3+ Months Ago

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?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

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

nl2br will convert line breaks to html <br> tags too.
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

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
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

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).
  • natas
  • PHP Ninja
  • Proficient
  • natas
  • Posts: 308
  • Loc: AFK

Post 3+ Months Ago

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?
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

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

Post 3+ Months Ago

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);
  • natas
  • PHP Ninja
  • Proficient
  • natas
  • Posts: 308
  • Loc: AFK

Post 3+ Months Ago

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.
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

Then that's what you're going to have to match on. Something like:
PHP Code: [ Select ]
$paragraph_markers = array("\n\n","\n");
  • natas
  • PHP Ninja
  • Proficient
  • natas
  • Posts: 308
  • Loc: AFK

Post 3+ Months Ago

So the user will have to input the "/n" when he enters the data?
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

"\n" is what gets stored every time the user hits the enter key.
  • natas
  • PHP Ninja
  • Proficient
  • natas
  • Posts: 308
  • Loc: AFK

Post 3+ Months Ago

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!

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 108 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.