user4252099
user4252099

Reputation:

How to print HTML code in php

I'm trying to develop a simple PHP page that allow me to print some data in a database. The problem is that some data aren't "clean", i think that in the data there are some html tag (i suppose).

An example of data is the following:

%3Ch1%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+51%29%3B%22%3EDirettore+Artistico%3C%2Fspan%3E%3C%2Fh1%3E

To solve my problem i tried this:

<?php
 $result = mysql_query("SELECT text FROM mutable WHERE (id_obj = 1)");
 while($row = mysql_fetch_array($result)){  
 echo "<p>" . html_entity_decode($row['text']) . "</p>";  
}

This solution don't work, i don't know if is important but the charset of my db is : latin1_swedish_ci

Upvotes: 1

Views: 148

Answers (3)

prem bhadouriya
prem bhadouriya

Reputation: 20

strong text

$string= "%3Ch1%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+51%29%3B%22%3EDirettore+Artistico%3C%2Fspan%3E%3C%2Fh1%3E";
echo urldecode($string);

?>

Upvotes: -2

user2917245
user2917245

Reputation:

its urlencoded html

Check this: urlencode($userinput) outputs:

%3Ch1%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+51%29%3B%22%3EDirettore+Artistico%3C%2Fspan%3E%3C%2Fh1%3E

And this: urldecode($userinput) outputs:

<h1><span style="color: rgb(0, 0, 51);">Direttore Artistico</span></h1>

EDIT:

From your question:

<?php
 $result = mysql_query("SELECT text FROM mutable WHERE (id_obj = 1)");
 while($row = mysql_fetch_array($result)){  
 echo "<p>" . html_entity_decode($row['text']) . "</p>";  
}

DO NOT use mysql_* functions. Use PDO instead.

Why shouldn't I use mysql_* functions in PHP?

Upvotes: 3

Halcyon
Halcyon

Reputation: 57709

If you don't know how this data is produced it's impossible to know what transformations to do to get the original. It looks like the data is urlencoded, that's easy enough to reverse with urldecode.

If you want the HTML to be interpretted you can do:

echo '<p>' . urldecode($row["text"]) . '</p>';
// result:
<p><h1><span style="color: rgb(0, 0, 51);">Direttore Artistico</span></h1></p>

If you want the HTML to show as it's plaintext you need to encode it:

echo '<p>' . htmlspecialchars(htmlenturldecode($row["text"])) . '</p>';
// result:
<p>&lt;h1&gt;&lt;span style=&quot;color: rgb(0, 0, 51);&quot;&gt;Direttore Artistico&lt;/span&gt;&lt;/h1&gt;</p>

Upvotes: 2

Related Questions