0

I need a function to prosses my textarea field input before I save it in my database.

Textarea field input:

Hi
sahdj sdhjsdj f hasjhf ash dsdhasd hjsdhjashdhsadj sjsadhjsdhjhassdnsdnnjas
Thanks

I want to make it look like this:

Hi<p></p>sahdj sdhjsdj f hasjhf ash dsdhasd hjsdhjashdhsadj sjsadhjsdhjhassdnsdnnjas<p></p>Thanks

Just want to replace line break with a <p></p>.

4
  • 3
    This requires output only formatting nl2br(); not database formatting Commented Apr 30, 2013 at 8:29
  • Welcome to Stack Overflow. You can format source code with the Code Sample {} toolbar button. Commented Apr 30, 2013 at 8:30
  • @Waygood How did I not know about nl2br()...?! I always did it manually (so thanks!) Commented Apr 30, 2013 at 8:32
  • preg_replace("/\r?\n/", '<p></p>', $string); Commented Aug 30, 2013 at 5:22

2 Answers 2

0

nl2br will only replace newline characters with <br />. On most of my sites I want to wrap the lines in p-tags. You could achieve this this way:

$lines = explode("\n",$_POST['textarea_name']);
$output = '';
foreach ($lines as $v) {
    $output .= '<p>'.$v.'</p>';
}
Sign up to request clarification or add additional context in comments.

Comments

-1

If you want to remove the HTML use the strip_tags() function against the $_POST variable from the textarea's name attribute in the HTML. e.g.:

$textarea_input = strip_tags($_POST['html_name_attrib']); 

The PHP Manual gives more info on that function. If you're talking about protection against SQL injection use the real_escape_string function for mysql or mysqli. Info here and example:

$protected_value = $mysqli_conn->real_escape_string($_POST['html_name_attrib']);

Did that answer your question?

You're not talking about using str_replace("\n", "<br/><br/>", $_POST['html_name_attrib']) to simply change the soft line breaks with HTML line breaks are you?

1 Comment

I think you mean using $array = explode("\n", $_POST['html_name_attrib']); and then foreach($array as $line){ $string .= '<p>' . $line . '</p>'; }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.