PHP HTMLenties允许<;b>;及<;i>;只有

PHP HTMLenties允许<;b>;及<;i>;只有,php,tags,html-entities,Php,Tags,Html Entities,使用htmlentities()是否有一种方法可以设置为只允许和转换为粗体和斜体文本?我知道有一种方法可以做到这一点,但我已经忘记了。这很容易 <?php $string = htmlentities($text); $string = str_replace(array("&lt;i&gt;", "&lt;b&gt;", "&lt;/i&gt;", "&lt;/b&gt;"), array("<i>", "<

使用
htmlentities()
是否有一种方法可以设置为只允许
转换为粗体和斜体文本?我知道有一种方法可以做到这一点,但我已经忘记了。

这很容易

<?php
$string = htmlentities($text);
$string = str_replace(array("&lt;i&gt;", "&lt;b&gt;", "&lt;/i&gt;", "&lt;/b&gt;"), array("<i>", "<b>", "</i>", "</b>"), $string);

我使用助手函数:

#   Sanitizer function - removes forbidden tags, including script tags
function strip_tags_attributes( $str, 
    $allowedTags = array('<a>','<b>','<blockquote>','<br>','<cite>','<code>','<del>','<div>','<em>','<ul>','<ol>','<li>','<dl>','<dt>','<dd>','<img>','<ins>','<u>','<q>','<h3>','<h4>','<h5>','<h6>','<samp>','<strong>','<sub>','<sup>','<p>','<table>','<tr>','<td>','<th>','<pre>','<span>'), 
    $disabledEvents = array('onclick','ondblclick','onkeydown','onkeypress','onkeyup','onload','onmousedown','onmousemove','onmouseout','onmouseover','onmouseup','onunload') )
{       
    if( empty($disabledEvents) ) {
        return strip_tags($str, implode('', $allowedTags));
    }
    return preg_replace('/<(.*?)>/ies', "'<' . preg_replace(array('/javascript:[^\"\']*/i', '/(" . implode('|', $disabledEvents) . ")=[\"\'][^\"\']*[\"\']/i', '/\s+/'), array('', '', ' '), stripslashes('\\1')) . '>'", strip_tags($str, implode('', $allowedTags)));
}
#消毒剂功能-删除禁止的标记,包括脚本标记
功能条\标签\属性($str,
$allowedTags=数组(“”,“”,“”,“
”,“”,“
”,“”,“
    ”,“”,“
  • ,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”,“,”, $disabledEvents=array('onclick'、'ondblick'、'onkeydown'、'onkeypress'、'onkeydup'、'onload'、'onmousedown'、'onmousemove'、'onmouseout'、'onmouseover'、'onmouseup'、'onunload')) { if(空($disabledEvents)){ 返回条带标签($str,内爆('',$allowedTags)); }
    return preg_replace('//ies',“'Thank,但这将离开并在字符串的末尾,当我试图添加它来替换它们时,它没有起作用:\@Dylan当然,我忘记了结束标记。现在再试一次