2

我必须为 full_name 字段使用什么 pregmach 规则?
我希望用户只输入字符而不是 html 或 php 代码值以及
我可以使用的 3 到 11 个字符之间的空格:

<?php
if (preg_match("%^[A-Za-z0-9-_]{3,10}$%", $_REQUEST['usr'])) {
//do something like 
mysql_query('insert into user(name) values(this field)');
//damn what is this for: It does not meet our quality standards.!!!
//i must insert more code? i dont have ! let me go !
}
else{
//do something else!
die('get out !:D');
}
?>

但是这个用户不能输入像“مسیحارسطوئی”
这样的UTF-8字符,那么我必须对UTF-8使用什么preg_match规则?
或者我可以使用什么其他代码,比如 preg_match ?
我希望用户可以
在 3 到 10 个字符之间插入不是 <>{}[] 或 $%^&* 的字符!!!谢谢

4

3 回答 3

2

像这样使用u修饰符:

preg_match('/pattern_with_unicode_symbols/u');

此修饰符打开与 Perl 不兼容的 PCRE 的附加功能。模式字符串被视为 UTF-8。

并使用 "\x{2460}" 语法定义 utf-8 字符

于 2013-01-24T23:16:09.960 回答
2

这将给出“0”,因为 cosمسیح ارسطوئی不仅包含 3-10 个字符;

$x = preg_match('~^([\pL]{3,10})$~u', 'مسیح ارسطوئی');
echo $x ? 1 : 0;

但这会在您的情况下给出结果;

preg_match('~([\pL]+)~u', 'مسیح ارسطوئی', $m);
print_r($m);

Array
(
    [0] => مسیح
    [1] => مسیح
)

在此处查看更多详细信息:PHP:Unicode 字符属性

于 2013-01-24T23:24:43.870 回答
1
preg_match_all('/#(\pL*)/u', 'this is #مثال text', $matches);
print_r($matches);

'u', '\pL':字符串被视为 UTF-8。

于 2014-03-30T09:04:33.700 回答