dongmeixian9665 2012-02-05 10:50
浏览 65
已采纳

正则表达式,用于检查表单中的有效输入

I am editing an existing project, and there was a bug being reported that the system doesn't accept if the numeric comes first in the input, like 99% Creative. But if you'll enter Creative 99%, the system accept it and save it in the database.

I check there existing code, and found out that they used this expression /^[\p{L}]+/u in preg_match. I googled over and found out to use \w but my senior wont accept my answer. He said its, unacceptable. This would give some issue.

Please check the script below, this is the one that i used:

$category= trim($_POST['category']);
if(preg_match('/\W/', $category)){
 $error='Invalid Input';
} 

He told me to test it, using some unicode character if it will accept like arabic texts, mixed (alphanumeric + arabic). It all works so it mean its fine but he still don't accept it. He told me to formulate another regex expression.

Do you have any idea what should i do? What could be the expression i can use. I really don't understand why \w is not acceptable.

  • 写回答

3条回答 默认 最新

  • duanqinjiao5244 2012-02-05 11:04
    关注

    The PHP manual on PCRE escape sequences says that in PCRE the characters matched by \w depend on the locale (and \W is the negation of \w).

    This is unacceptable in a global environment, as you do not know the used server locale. What was the last setlocale() call? Is the site hosted in the U.S.A., in France, in China? You never know. And what might apply today, might not tomorrow. Users change, and sites do move.

    For example, your senior's server might as well use a different locale than your server. So they could be right saying it does not work for them with certain characters that are not in their current locale.

    That is why they cannot accept your solution. You need to use Unicode character properties instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源