PHP does not generating output as html

May 13, 2011 | In: HTML, php, web development

In many cases we add content in the database with replacing all html tags into special characters. But it generate problems when we want to show at front side. So here we are giving a best solution to show result as HTML:

function __htmlConvertor($str)
{
$string = str_replace(array("<", ">", '&', ''', '"','<', '>'), array("<", ">",'&','\'','"','<','>'), htmlspecialchars_decode($str, ENT_NOQUOTES));
 
return $string;
}


Above function takes the string from the database (manually send)
and will return the code in the html format. Here are giving an example so you can understand in better way:

Before
<div>
naveenos group
</div>
<table>
<tr>
<td>http://naveenos.com</td>
</tr>
</table>

 

After called to this function:

<div>
naveenos group
</div>
<table>
<tr>
<td>http://naveenos.com</td>
</tr>
</table>