utf-8的preg_match规则 [英] preg_match rule for utf-8

查看:99
本文介绍了utf-8的preg_match规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须对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个字符之间! 谢谢

解决方案

这将给出"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字符属性

what pregmach rule i must use for full_name field?
i want user input only character and not html or php code value and space between 3 to 11 character
i can use:

<?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');
}
?>

but with this user cant input UTF-8 characters like "مسیح ارسطوئی"
so what preg_match rule i must use for UTF-8 ?
or what other code like preg_match i can use ?
I WANT USER JUST Can insert characters not <>{}[] or $%^&*
between 3 to 10 character !!! thanks

解决方案

This will give "0", cos مسیح ارسطوئی is not containing only 3-10 chars;

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

But this gives a result in your case;

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

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

See more details here: PHP: Unicode character properties

这篇关于utf-8的preg_match规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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