简体   繁体   English

strip_tags足以从字符串中删除HTML?

[英]strip_tags enough to remove HTML from string?

The site user can sign-up on a site, and during sign-up he can provide a name. 站点用户可以在站点上注册,并且在注册期间可以提供名称。

I want this name to be a valid name, and free of any HTML and other funky characters. 我希望此名称是有效名称,并且不包含任何HTML和其他时髦字符。 Is strip_tags enough for this? strip_tags够吗?

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

Regex could fit well with less code: 正则表达式可以用更少的代码很好地适应:

^[AZ]'?[- a-zA-Z]( [a-zA-Z])*$

Here we have good examples: 这里有很好的例子:

Regex for names 正则表达式的名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM