Saving \n as <br> to database from textarea [duplicate] - php

This question already has answers here:
Preserve Line Breaks From TextArea
(7 answers)
Closed 7 years ago.
I tried this but it's not chaning new lines to < br >
<textarea name="addesc" id="addesc"><?php echo $data['addesc']; ?></textarea>
$data['addesc'] = preg_replace("/\r\n|\r/", "<br />", $_POST["addesc"]);
$data['addesc'] = trim($data['addesc'])
As such there's no $_POST["addesc"] on the post.php page. There's $_POST["do"]. I tried it too. No results.

There is a php function for that called nl2br.
Btw. searching for "new line to br" would have been very easy and brings up certain related topics on SO and the php manual of nl2br:
How to replace \r & \n with <br/>?

Related

PHP echo long text [duplicate]

This question already has answers here:
Preserve Line Breaks From TextArea
(7 answers)
Closed 4 years ago.
This is inside the phpmyadmin:
This is the output after echo:
and this is the code for display output
$joinEvent = mysqli_query($db, "SELECT * FROM event WHERE event.eventID='".$_GET['eventID']."'");
while($row = mysqli_fetch_array($joinEvent))
{
$joinDetails = $row['details'];
}
<p align="justify"><?php echo $joinDetails; ?></p>
How do I echo exactly what user enter in the input?
Use nl2br() when you output to the screen, change your current code to:
<p align="justify"><?php echo nl2br($joinDetails); ?></p>
It might be better for you to store your details column as a text datatype.
Read here: What is the MySQL VARCHAR max size?
Your result is actually exactly what the user input is ; except that in HTML, line break characters (\r\n \n and so) are not interpreted by the browser. As said in the comments, you'll have to use the PHP function nl2br() to convert your line break characters to <br /> tags, which are the HTML line breaks.

How to retrieve and echo text from database while keeping format [duplicate]

This question already has answers here:
Preserve Line Breaks From TextArea
(7 answers)
Closed 4 years ago.
In my PHP file I am able to retrieve text from my MySQL database, but it isn't in the same format when I echo it.
The image attached shows how the text is in the database, and how I want it to be echoed.
But instead, it echoes it without the line breaks. How can I keep the format?
Use the <pre> tag to output pre-formatted text, so it won't be reformatted:
echo "<pre>" . $row['content'] . "</pre>";
Or use the nl2br() function to convert newlines to HTML:
echo nl2br($row['content']);

SQL Insert line break [duplicate]

This question already has answers here:
Adding a line break in MySQL INSERT INTO text
(12 answers)
Closed 9 years ago.
How can i insert line break by a variable inserting into mysql?
$delattn = $row_Recordset_delattn['Cus_fname'].' '.$row_Recordset_delattn['Cus_lname'].',
'.$row_Recordset_delattn['Cus_designation'].',
LINE BREAK
'.$row_Recordset_delattn['Cus_department'];
Simply add a "\n" where you want a line break
$delattn = $row_Recordset_delattn['Cus_fname'].' '.$row_Recordset_delattn['Cus_lname'].','.$row_Recordset_delattn['Cus_designation'].', \n '.$row_Recordset_delattn['Cus_department'];
Use PHP's
nl2br()
function to translate that line break into a
<br />
tag in HTML. More info here

Php nl2br show /r/n [duplicate]

This question already has answers here:
How to use nl2br() to handle string with '\r\n'?
(4 answers)
Closed 8 years ago.
When I use the function nl2br like result I see /r /n
example:
in a textarea I wrote
Hello,
how are you?,
fine
php
echo nl2br($_POST['message']);
result to screen
\r\nHello,
\r\nhow are you?
\r\nfin
how can I fix it? thank you
try this:
$newmessage = preg_replace('#(\\\r|\\\r\\\n|\\\n)#','<br />', $_POST['message']);

How to wrap a long text having no spaces in php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to wrap long lines without spaces in HTML?
I have a long text "jkjkllllllllljcsccncnmchdkcjhvcjdkvk".how to wrap this text in php
Use this:
<?php
$iamlong = "woooooooooooord.";
$iamwrapped = wordwrap($iamlong , 8, "<br>", true);
echo $iamwrapped;
?>

Categories