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(' ', '', $output);
return $output;
Any better ideas would be much appreciated.
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);
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;
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With