Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP htmlentities() without converting html tags

I've found a few posts which refer to the problem, but none of them fully resolve it.

I need the function which will output the content converting all special characters in the way the htmlentities() would, but preserving all html tags.

I've tried many different approaches, but as I've mentioned above - none of them works as expected.

I was wondering whether there would be a way of doing it using PHP class DomDocument.

I've tried to do it using the following:

$objDom = new DOMDocument('1.0', 'utf-8');
$objDom->loadhtml($content);
return $objDom->savehtml();

which works, but it also adds the entire structure of the page i.e.

<head><body> etc.

I only need the content of the $content variable to be converted and job done.

Another thing worth to mention here is that $content might also have some characters converted to xhtml complaint - as it comes from Wysiwyg. So it might containt & etc., which should also be preserved.

Anyone knows the way to do it with DomDocument - perhaps I should use different save method?

Ok - I've come up with the following - not great, but does the job spot on:

$objDom = new DOMDocument('1.0', 'UTF-8');
$objDom->loadHTML($string);
$output = $objDom->saveXML($objDom->documentElement);
$output = str_replace('<html><body>', '', $output);
$output = str_replace('</body></html>', '', $output);
$output = str_replace('&#13;', '', $output);
return $output; 

Any better ideas would be much appreciated.

like image 992
user398341 Avatar asked Jun 13 '11 14:06

user398341


2 Answers

You could use get_html_translation_table and remove the < and > items:

$trans = get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES);
unset($trans['<'], $trans['>']);
$output = strtr($input, $trans);
like image 79
Gumbo Avatar answered Nov 04 '22 04:11

Gumbo


get_html_translation_table(HTML_ENTITIES) gives you the translation table used in htmlentities() as an array. You can remove <, > and " from the array like so:

<?php
$trans = get_html_translation_table(HTML_ENTITIES);
unset($trans["\""], $trans["<"], $trans[">"]);
$str = "Hallo <strong>& Frau</strong> & Krämer";
$encoded = strtr($str, $trans);

echo $encoded;
?>
like image 22
Kyle Kochis Avatar answered Nov 04 '22 04:11

Kyle Kochis