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条)

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重