PHP闯入新行[重复]

This question already has an answer here:

I have my textarea data called from db, which data expected to show in new line without <br /> along, how can I break the <br /> and show in textarea into a new line?

My database column is stored as hello<br />world exactly as well.

enter image description here

Thanks.

</div>

use:

$text = 'This is some text<br />With a line break in it';

$text = str_replace('<br />', "
", $text);

// Outputs: 
   This is some text
   with a line break in it

Make sure the parameter in str_replace is between double quotes. It wont work between single quotes.

If it is only for new lines, you can use str_ireplace

$text = str_ireplace("<br />", "
", $text);
$text = str_ireplace("<br>", "
", $text);

what about

str_replace('<br />', PHP_EOL, 'hello<br />world')

The way I do this in HTML5 is by putting a lot of spaces between line 1 and line 2.

It's a little bit of a dirty hack, but it works.

<textarea rows="8" placeholder="Line 1                                     
                                                                        Line 2
                                                                        Line 3"></textarea>

Storing this many white-spaces in your database might not be ideal but I don't see why it wouldn't work.

What I like to do is defining multi-line strings in arrays like that:

$output = array();
$output[] = 'Hello<br />';
$output[] = 'World.<br />';
$output[] = '<br />';
$output[] = 'This is a test.';

echo implode("
", $output);

This will return the HTML code:

Hello<br />
World.<br />
<br />
This is a test.

What you should do is NOT store HTML inside your database in the first place.

  1. Store text in the database, keeping newlines as " ".

  2. Upon rendering on the page, use nl2br(htmlspecialchars($str, ENT_QUOTES, 'UTF-8')).

You could use urlencode and urldecode which I use for a similar thing, for example:

Store your data in the database like this:

$html       = urlencode($_POST['content']);

with a normal query like this:

INSERT INTO table (Content) VALUES ('".$html."')

And then get it out of the DB using this:

$strSQL1    ="SELECT Content FROM table;
$objQuery1  = mysql_query($strSQL1);
$objResult1     = mysql_fetch_array($objQuery1); 

$show       = urldecode($objResult1["Content"]); 

And then populate the textarea with this:

<textarea name="content"><?php echo "$show"?></textarea>