PHP htmlentities()无需转换html标签 [英] 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.
我尝试了许多不同的方法,但是正如我上面提到的,没有一个方法可以使用htmlentities()那样的特殊字符。他们正在按预期的方式工作。
I've tried many different approaches, but as I've mentioned above - none of them works as expected.
我想知道是否可以使用PHP类DomDocument来做到这一点。
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.
我只需要转换$ content变量的内容并完成工作。
I only need the content of the $content variable to be converted and job done.
这里值得一提的另一件事是$ content可能还会将某些字符转换为xhtml投诉-因为它来自Wysiwyg。因此它可能包含&
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.
任何人都知道使用DomDocument的方法-也许我应该使用其他保存方法?
Anyone knows the way to do it with DomDocument - perhaps I should use different save method?
好-我提出了以下内容-不太好,但是工作地点在以下位置:
$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.
推荐答案
好-经过大量研究,我提出了最终的选择-这似乎正是我所需要的。
Ok - after a lot of research I've come up with the final option - which seem to be just what I needed.
我使用了 HTMLPurifier 并使用以下内容过滤了我的内容:
I've used the HTMLPurifier and filtered my content using the following:
require_once('HTMLPurifier/HTMLPurifier.auto.php');
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
$objPurifier = new HTMLPurifier($config);
return $objPurifier->purify($string);
我希望其他人会觉得有用。
I hope someone else will find it useful.
这篇关于PHP htmlentities()无需转换html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!