strip_tags足以从字符串中删除HTML? [英] strip_tags enough to remove HTML from string?

查看:123
本文介绍了strip_tags足以从字符串中删除HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网站用户可以在网站上注册,在注册期间,他可以提供一个名称。

The site user can sign-up on a site, and during sign-up he can provide a name.

我希望这个名称是一个有效的名称,并没有任何HTML和其他时髦的字符。是否strip_tags足够这个?

I want this name to be a valid name, and free of any HTML and other funky characters. Is strip_tags enough for this?

推荐答案

我发现没有单一的功能用于防白痴用户输入。最好混合一些:

I find that there's no single function for idiot-proofing user inputs. Best to mix a few together:

$val = trim($val);
$val = strip_tags($val);
$val = htmlentities($val, ENT_QUOTES, 'UTF-8'); // convert funky chars to html entities
$pat = array("\r\n", "\n\r", "\n", "\r"); // remove returns
$val = str_replace($pat, '', $val);
$pat = array('/^\s+/', '/\s{2,}/', '/\s+\$/'); // remove multiple whitespaces
$rep = array('', ' ', '');
$val = preg_replace($pat, $rep, $val);
$val = trim($val);
$val = mysql_real_escape_string($val); // excellent final step for MySQL entry

这篇关于strip_tags足以从字符串中删除HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆