HTML not interpreting tags - php

I am saving data into mysql using htmlspecialchars(). On output when getting data using htmlspecialchars_decode() before displaying it in Angular. But instead of interpreting the html code it is displaying it as a text.
Data in Database
<p class="fr-tag"><span>test</span></p>
Code being displayed after htmlspecialchars_decode()
<p class="fr-tag"><span>test</span></p>
Displaying the output from database into div as follows
<div *ngIf="product.pdescription" class="mt-2 product-description">{{product.pdescription}}</div>
I have also tried using html_entity_decode but didn't help.
Following is a screenshot of the browser

In order to make it working you should use innerHTML or outerHTML binding:
<div *ngIf="product.pdescription" ... [innerHTML]="product.pdescription"></div>
But beware about some limitations

Related

How to save raw html to sql with codeigniter?

I'm working with Ionize - i'm trying to save text with html tags to mysqli, but i keep getting the text like this :
<p><div class="foo">foo</div></p>
while i want to get
<div class="foo">foo</div>
I've tried to replace all the htmlspecialchars methods with html_entity_decode
but it didn't worked
It's ok to have it saved like that, you just have to apply htmlspecialchars_decode on your result before printing it.

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.

HTML doesn't get rendered, why does it happen?

On a PHP+MySQL project, there's a string of text coming from a MySQL table that contains HTML tags but those tags never get rendered by Google Chrome or any browser I tried yet:
You can see that the HTML (p, strong) aren't getting interpreted by the browser.
So the result is:
EDIT: HTML/PHP
<div class="winery_description">
<?php echo $this->winery['description']; ?>
</div>
$this->winery being the array result of the SQL Select.
EDIT 2: I'm the dumbest man in the world, the source contains entities. So the new question is: How do I force entities to be interpreted?
Real source:
Any suggestions? Thanks!
You are probably using innerText or textContent to set the content of your div, which just replace the child nodes of the div with a single text node.
Use innerHTML instead, to have the browser parse the HTML and create the appropriate DOM nodes.
The answer provided by #Paulpro is correct.
Also note that if you are using jQuery, be sure to use the .html() method instead of .text() method:
$('#your_element').html('<h1>This works!</h1>');
$('#another_element').text('<h2>Wrong; you will see the <h2> in the output');

How to fix html string data in PHP and Mysql

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

Transform html tags from db

I am using a jquery editor.And the content will be saved in the database along with the html tags . That is for example I have typed "aneesh" in the editor and using editor makes the text bold and italics the saved the content to db .
So the content will be saved in the db along with text and html tags
Now i want take the content from db and want show the text in formated ways that is need to transform the html tags that in my the content want to show like this aneesh Is there any way for this please help me
To store and retrieve html you should use htmlspecialchars() - this preserves html tags etc etc when saving and htmlspecialchars_decode() - this converts them back to proper html that can be displayed - when displaying the retrieved content.
Storing using this and calling the decode function when populating the textarea behind the wysiwig editor will/should have kept everything preserved.
Or you can also use htmlspecialchars()
$a=htmlspecialchars($a);
<text>$a</text>
wud help u save and retrieve the html characters
Just select the data out of database (i'm using PDO): and echo/post the result to the document without string formating:
$sql = 'SELECT * FROM table;';
$stmt = $DB->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['result'];
if you have content with html tags like i-e <b>Hello</b> in the database, and when you return it from the database again, the jquery editor that you are using will automatically remove those tags and will transform your text again and make it i-e bold (if hello in case).
There is no need to do anything but just echo it out.
you need to use javascript for better and easy solution.
get raw html content from db (with php) to show web browser (with javascript): innerHTML or .html() (JQuery)

Categories