我目前已经使用此功能来清理用户提交的输入:
// Cleaning input functions
function clean_Input($input) {
$search = array(
'@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<['/'!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<!['s'S]*?--[ 't'n'r]*>@' // Strip multi-line comments
);
$output = preg_replace($search, '', $input);
return $output;
}
function sanitize($input) {
if (is_array($input)) {
foreach ($input as $var => $val) {
$output[$var] = sanitize($val);
}
} else {
if (get_magic_quotes_gpc()) {
$input = stripslashes($input);
}
$input = clean_Input($input);
$output = mysql_real_escape_string($input);
}
return $output;
}
但是,我想对它进行编辑,使其允许<a>
和<b>
标签通过。这意味着它将接受链接和粗体标签,并且不会删除它们。
目前,它删除了所有的HTML标记,包括<a>
标记和<b>
标记,我如何更改它来完成我上面所说的?
如果我知道如何使用preg_replace
函数,我会尝试一些方法。然而,我完全被卡住了,完全不知道从这里该去哪里。
使用strip_tags并向其传递一些允许的标记,从而传递passthrough有什么错误吗?为什么选择Regex?
请考虑手册页面上的示例:
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');