This question already has answers here:
Preserve Line Breaks From TextArea
(7 answers)
Closed 10 years ago.
I have a form containing text area. I am entering text in this manner -
Hi Dear,
How are you .
Just reply when you get my message.
I am using mysql database, in that its storing the same manner.
But when I am displaying it, its just displaying in single line.
like -
Hi Dear, How are you . Just reply when you get my message.
But I don't want it like this, I want to display it in the same manner as it stored in the database.
If anyone know the solution of this reply me please.
Thank you
Since you are displaying this in a text area, and this lines are stored as a single value in the db, then they should be ended by <br /> like this:
Hi Dear, <br />
How are you .<br />
Just reply when you get my message.
You need to replace
\n\r or \n or \r
By:
<br />
PHP function:
nl2br()
ELSE
$content = str_replace("\r\n", "<br />", $content);
Use the php function nl2br($input) on your text input before it goes into the database. It will replace line breaks with <br /> tags.
Related
This question already has answers here:
get user name from an E-mail address in php
(6 answers)
Closed 2 years ago.
I have an html code which displays user email from php. However i want to trim this part #gmail.com
And display the left part.
Currently I tried this code which is not working.
<p id="p1"><?php
$str = ""
echo $str . "<br>";
echo trim($str,"#gmail.com");
echo htmlspecialchars($_SESSION["username"]);
?></p>
What is the best way to correct it?
For example the email from php database is name1#gmail.com, i want to remove #gmail.com and display only name1
trim() is not the best choice. Simply split email by #:
$expl = explode('#', $str);
echo $expl[0];
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.
This question already has answers here:
how to show the content exactly as it's saved in mysql
(4 answers)
Closed 6 years ago.
I have a form with an input textbox that stores the content in the mysql database.
For example, I write in the input box:
Hello World!
Here is the new line
In the database, I find exactly the same text without any special characters ("here is..." starts in a new line in the database too).
When I call this data in a php echo, it shows it as:
Hello World! Here is the new line
What's wrong? How can I fix it? thanks
You need to pass the database string to nl2br fucntion to get the new line as break rule in HTML.
<?php
$dbSQLValue = "Hello World!\nHere is the new line"; //string retrieved from SQL
echo nl2br($dbSQLValue);
?>
New lines are not converted directly to HTML break line.
Hope this helps.
Open page source in browser and see that newlines are there, but for the browser to display them - they should not be \ns, but <br> tags or be inside a <pre> tag.
So: either convert newlines to brs via nl2br($your_var) or put the whole echo inside a <pre>.
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/>?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to properly escape html form input default values in php?
I am facing problem on facing </textarea> element.
I am working on whole php file edit. If the php file has </textarea> tag it closes the files codes.
E.g.
$data=file_get_contents($file);
..
'<textarea>'
'.$data.'
'</textarea>'
..
The problem is:
if contain the </textarea> tag in data, my codes truncted by the tag. Because </textarea> is end tag of textarea values. Any solve there to not execute </textarea> tag(which contained $data)?
Try cleaning up $data before outputting it, such as:
'.htmlentities($data).'
Also, take a look at the available flags for the function in the PHP documentation.