php echo not showing new lines stored in mysql [duplicate] - php

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>.

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']);

Why do I always get the error "Too few arguments" whenever I try to insert contents into my template using sprintf in Php? [duplicate]

This question already has answers here:
[PHP]: Error -> Too few arguments in sprintf();
(2 answers)
Closed 6 years ago.
I have a language.inc file which has a email template that looks like this:
$lang["email_templete"] = '
<style type="text/css">
*....more stylings here...*
</style>
<div>
<table>
*....more elements....*
%s
</table>
</div>
';
My problem is that whenever I'm trying to insert contents into my template using sprintf, it always hits the error "Too few arguments". I've tried removing all the excessive html codes and replaced it with a more simple html code and it works. Do you have any clue on this problem?
function add_to_email_template($pTargetEmail, $pSubject, $pContent) {
global $lang;
$vMessage = sprintf($lang["email_templete"], $pContent);
send_email($pTargetEmail, $pSubject, $vMessage);
}
Read the manual page for sprintf() and you'll see that the % sign is a format modifier of which your HTML and CSS code is filled with.
If you want to include % inside the string without getting it parsed, you need to escape it with itself such as %% will output %.
Note that it would probably be much simpler to user variables in your string definition than to use sprintf() the way you do, such as:
$variable = 'abc';
$myHTML = "Here is my variable: $variable";

file_get_html(), returning -> plaintext with another encoding [duplicate]

This question already exists:
Getting special chars from file_get_html, even if i save it as ->plaintext [closed]
Closed 9 years ago.
Okay, i have spend alot of time on this issue, i want to get a tag from a exsternal page, so i do this:
$dom = file_get_html($url);
$name = $dom->find('h1');
$name = $name[0]->plaintext;
The website displays the content of the h1 tag as: Grandon Multi-Purpose HTML Template
When i echo $name it is: Grandon Multi-Purpose HTML Template
But when i save it in MySQL it becomes:
Grandon Multi-Purpose HTML Template
When i save the source of the page it also shows:
Grandon Multi-Purpose HTML Template
Is there any quick way to replace those special chars with their actual values ?
Yes. Use html_entity_decode():
$name = html_entity_decode($name[0]->plaintext);

Using file_get_html(); is returning the HTML with special chars even when i use ->plaintext [duplicate]

This question already exists:
Getting special chars from file_get_html, even if i save it as ->plaintext [closed]
Closed 9 years ago.
Okay, i have spend alot of time on this issue, i want to get a tag from a exsternal page, so i do this:
$dom = file_get_html($url);
$name = $dom->find('h1');
$name = $name[0]->plaintext;
The website displays the content of the h1 tag as: Grandon Multi-Purpose HTML Template
When i echo $name it is: Grandon Multi-Purpose HTML Template
But when i save it in MySQL it becomes:
Grandon Multi-Purpose HTML Template
When i save the source of the page it also shows:
Grandon Multi-Purpose HTML Template
Is there any quick way to replace those special chars with their actual values ?
Yes. Use html_entity_decode():
$name = html_entity_decode($name[0]->plaintext);

Categories