Better strip_tags

databind
vitalif 2013-06-01 11:55:38 +00:00 committed by Vitaliy Filippov
parent e9b3a3274d
commit 6e71eb1321
2 changed files with 10 additions and 3 deletions

View File

@ -375,11 +375,11 @@ $code
function function_html($e) { return "htmlspecialchars($e,ENT_QUOTES)"; }
/* удаление всех или заданных тегов */
function function_strip($e, $t='') { return "strip_tags($e".($t?",$t":"").")"; }
function function_strip($e, $t='') { return "self::strip_tags($e".($t?",$t":"").")"; }
/* удаление "небезопасных" HTML-тегов */
/* TODO: м.б исправлять некорректную разметку? */
function function_strip_unsafe($e) { return "strip_tags($e, self::\$safe_tags)"; }
function function_strip_unsafe($e) { return "self::strip_tags($e, self::\$safe_tags)"; }
/* заменить \n на <br /> */
function function_nl2br($s) { return "nl2br($s)"; }

View File

@ -52,7 +52,7 @@ class VMXTemplate
static $Mon, $mon, $Wday;
static $cache_type = NULL;
static $cache = array();
static $safe_tags = '<div> <blockquote> <span> <a> <b> <i> <u> <p> <h1> <h2> <h3> <h4> <h5> <h6> <strike> <strong> <small> <big> <blink> <center> <ol> <pre> <sub> <sup> <font> <br> <table> <tr> <td> <th> <tbody> <tfoot> <thead> <tt> <ul> <li> <em> <img> <marquee> <cite>';
static $safe_tags = 'div|blockquote|span|a|b|i|u|p|h1|h2|h3|h4|h5|h6|strike|strong|small|big|blink|center|ol|pre|sub|sup|font|br|table|tr|td|th|tbody|tfoot|thead|tt|ul|li|em|img|marquee|cite';
// Timestamp format constants
const TS_UNIX = 0;
@ -612,6 +612,13 @@ class VMXTemplate
return mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 0, 1);
}
// Replace tags with whitespace
static function strip_tags($str, $allowed = false)
{
$allowed = $allowed ? '(?!/?('.$allowed.'))' : '';
return preg_replace('#(<'.$allowed.'/?[a-z][a-z0-9-]*(\s+[^<>]*)?>\s*)+#is', ' ', $str);
}
// Ignore result
function void($a)
{