public function HTML($content) { include_once APPPATH . 'controllers/api/base_class_html.php'; $HTML = new HtmlAttributeFilter(); $HTML->setAllow(array('title', 'alt', "src", 'style', 'href')); $content = $HTML->strip($content); return strip_tags($content, "<span><em><i><strike><u><b><strong><p><li><ul><img><br><a>"); }
* @return String */ private function createAttributes($new_attributes, $name, $value) { if ($new_attributes) { $new_attributes .= " "; } $new_attributes .= "{$name}=\"{$value}\""; return $new_attributes; } /** 特殊字符转义 * @param String $str 源字符串 * @return String */ private function protect($str) { $conversions = array("^" => "\\^", "[" => "\\[", "." => "\\.", "\$" => "\$", "{" => "\\{", "*" => "\\*", "(" => "\\(", "\\" => "\\\\", "/" => "\\/", "+" => "\\+", ")" => "\\)", "|" => "\\|", "?" => "\\?", "<" => "\\<", ">" => "\\>"); return strtr($str, $conversions); } } $str = '<div class="bd clearfix" id="index_hilite_ul"><ul class="list"><li><img src="http://su.bdimg.com/static/skin/img/logo_white.png" width="118" height="148"><div class="cover"><a class="text" href="http://www.csdn.net"><strong>yuna</strong><p>love</p></a><strong class="t g">want to know</strong><a href="/login.html" class="ppBtn"><strong class="text">YES</strong></a></div></li></ul></div>'; $obj = new HtmlAttributeFilter(); // 允许id属性 $obj->setAllow(array('id')); $obj->setException(array('a' => array('href'), 'ul' => array('class'))); // img 标签忽略,不过滤任何属性 $obj->setIgnore(array('img')); echo 'source str:<br>'; echo htmlspecialchars($str) . '<br><br>'; echo 'filter str:<br>'; echo htmlspecialchars($obj->strip($str));