How to fix html string data in PHP and Mysql - php

I am saving html data into mysql
like below
<p><div id="aaa">asdadad</div></p>
<p><div id="aaa">asdadad</div></p>
<div class="something">some data</div>
when showing the html as string in front end using php , it coverts all 3 lines(above) to html content not just string
But I expect the below result
first 2 line as string and 3rd line as html
I used:
html_entity_decode($content_from_db, ENT_QUOTES, 'utf-8');

Don't do anything to the data, just print it straight to the browser. If the data is stored as you say then you will get expected behaviour. If you don't get expected behaviour then either:
Data is not stored correctly, view it in MySQL console or a RDBMS
You are post-processing data after it is being pulled from DB

Related

mysql stored value displayed as raw html in vue through php

I have a value stored as <b> supposed to be bold </b> in mysql.
while fetching through php I used nearly all html entity related functions but no avail
//will be returned as json for vue
$item ['value'] = $fetched_data;
in vue template I use like this
{{$value}}
and it gives me as html tags where I am supposed to print the bolded text, anything should I do in PHP or Vue to make this happen?
You can use v-html="value" on the element which needs to contain your value.
https://v2.vuejs.org/v2/guide/syntax.html#Raw-HTML

Converting html for mysql - what to do with <br/>s?

I’m converting a number of html documents into csv files to upload into MySQL – I’m replacing html div’s etc with tabs as necessary in a text editor to get the data into the columns, then pasting into a spreadsheet and saving as CSV. This is working fine
A couple of ‘fields’ have data on more than one line, which in html is achieved by using br’s, leaving these in the CSV displays the data in the required format.
I’ve also produced a script to add/amend data and for the multi line fields I’m using textareas and then outputting using nl2br() which again displays the data as required. But the uploaded data with br’s is displayed in the add/amend script as a continuous line with the br’s.
Question – Is there anything I can do when manipulating the html data to replace the br’s so that they appear as multi lines in the textareas ?
Replace with "\n"
<?
$string="Some stuff <br/> some other";
$string=str_replace("<br/>","\n",$string);
?>
<textarea><?=$string?></textarea>
You can replace with \n instead of br tag

Echo html from a database

I am attempting to write html data from a mysql database to a document using php. My code is below:
$content = html_entity_decode($dataToLoad['Text']);
echo $content;
$dataToLoad['Text'] contains this text data from the database:
<div>stuffInDiv</div>
What I would like to happen is for this text to be written as an actual div element in the document, but instead it is being written as a string. How can I force php to write it as an element?
Update for clarity:
To clarify, my issue isn't with decoding the html entities in the database, it's with writing the decoded html to the document. When I do:
echo $content;
where $content contains
<div>stuffInDiv</div>
I get the string "<div>stuffInDiv</div>" when really what I want to have is a div containing the string "stuffInDiv"
The Answer
It's possible your data has been encoded twice. Try echo $content; and then go to View Source in your browser. If it starts with &lt;div&gt;, then you'd need to run html_entity_decode twice. There is rarely a good reason, however, to store the HTML in the database with the entities encoded. It'd make more sense to store it raw and encode it when need be (e.g. if the code were placed into a textarea).
$content = html_entity_decode(html_entity_decode($dataToLoad['Text']));
echo $content;
The Reasoning
The reason is because the raw data in your database looks like this:
&lt;div&gt;stuffInDiv&lt;/div&gt;
Your browser would print this on the screen:
<div>stuffInDiv</div>
The first time you run html_entity_decode, it does exactly that, i.e. it replaces & with the & character (& is the code for the ampersand).
This produces:
<div>stuffInDiv</div>
The web page spits out the encoded entities, i.e.:
<div>stuffInDiv</div>
Running html_entity_decode a second time would replace < with < (less than sign), > with > (greater than sign), etc.
This produces:
<div>stuffInDiv</div>
Which would be outputted to your page as:
stuffInDiv
Your Database Setup
As a note to your database:
When storing information in the database, do not encode the HTML at all. Unless HTML is being outputted onto a web page, it is no different from any other string and you shouldn't treat it differently. So if you were adding data to a table in your database that contains code, just do something like this:
INSERT INTO `my_content` (`name`, `content`) VALUES ("My Page", "<div>stuffInDiv</div>");
If you were obtaining this data from a textarea, use:
$connection->query('INSERT INTO `my_content` (`name`, `content`) VALUES ("'.$connection->real_escape_string($_POST['name']).'", "'.$connection->real_escape_string($_POST['content']).'");');
Without doing anything to manipulate the value of $_POST['content']. If you need to place that data back into the textarea (say, editing a page):
$result = $connection->query('SELECT `content` FROM `my_content` WHERE `name` = "'.$connection->real_escape_string($_GET['edit_page']).'");');
if ($row = $result->fetch_assoc()) {
print '<textarea name="content">'.htmlentities($row['content']).'</textarea>';
}
You can replace html entities with str_replace... (but surely there's an easier way)
$ar = get_html_translation_table();
$dataToLoad['Text'] = '<div>stuffInDiv</div>';
echo str_replace($ar, array_keys($ar), $dataToLoad['Text']);

Textarea content to database

I have this textarea called personalInfos where i fill the infos in following format :
<p><span class="white">1966 - '69</span><br/> text .... </p>
When i submit it to database, it gets saved ok, same format. When i retrieve the code from database to admin textarea it gets filled ok.
My only problem is on front end where i get displayed the code as text not rendered as html code. So basiclly i see it on the page like this :
<p><span class="white">1966 - '69</span><br/>
Most likely you display fetched code parsed processed by htmlentities() or similar function. This is in most cases the way to go to avoid planting i.e. html in comments. So simply stop doing this after fetching (or insert - depends where you do so) and your content will be outputed as literaly HTML and properly processed by web browser.
You should have a look at htmlspecialchars_decode()
Example
$str = '<p><span class="white">1966 - \'69</span><br/> text .... </p>';
echo htmlspecialchars_decode($str);
Also make sure to escape the single quotes as well.

Data retrieved from database ignoring line breaks

Data that is stored in my mysql database, has the breaks of text input properly, but when retrieved the data no longer has the breaks, and everything is displayed as one string without any <br>. Any idea why this would occur?
The data column is the format text,
for example: in a table there is:
hey
how
do
you
do
After retrieving the data ill echo $mesarray[0][message]; and the result is:
hey how do you do
I want the line breaks to appear, but they dont.
The reason is because HTML does not understand line breaks. They need <br /> tags to break lines.
There is a function called nl2br() which can be used to convert new lines to <br>
echo nl2br($mesarray[0][message]);

Categories