dtxob80644 2012-11-20 11:02
浏览 67
已采纳

php preg匹配a-zA-Z并且在2个或更多单词之间只有一个空格

For my PHP script I have this code:

if (!preg_match("/[^A-Za-z]/", $usersurname))
   $usersurname_valid = 1; 

This worked untill I realized a surname can be two or more words... doh.

Anyone can tell me how to write this code if I want to allow 1 space between two worlds? For example:

Jan Klaas is now wrong and Jan Klaas should be allowed, also Jan Klaas Martijn and so on should be allowed.

Even better would be a preg replace, to replace two or more spaces with 1, so when you write:
Jan(space)(space)Klaas or Jan(space)(space)(space)(space)Klaas, it would return Jan(space)Klaas.

I searched around for a while but somehow I just can't get this space matching to work..

PS: When I got this working, I will apply this for the mid and last name too ofcourse.

===========================================

EDIT: After you helping me out, I re-wrote my code to:

// validate usersurname
$usersurname = preg_replace("/\s{2,}/"," ", $usersurname);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usersurname))
    $usersurname_valid = 1; 
// validate usermidname
$usermidname = preg_replace("/\s{2,}/"," ", $usermidname);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usermidname))
    $usermidname_valid = 1;
// validate userforename
$userforename = preg_replace("/\s{2,}/"," ", $userforename);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$userforename))
    $userforename_valid = 1;

and the error notifications

elseif ($usersurname_valid !=1)
    echo ("<p id='notification'>Only alphabetic character are allowed for the last name. $usersurname $usermidname $userforename</p>");
// usermidname character validation
elseif ($usermidname_valid !=1)
    echo ("<p id='notification'>Only alphabetic character are allowed for the middle name. $usersurname $usermidname $userforename</p>");
// userforename character validation
elseif ($userforename_valid !=1)
    echo ("<p id='notification'>Only alphabetic character are allowed for the (EDIT) first name. $usersurname $usermidname $userforename</p>");

Replacing the spaces are working well and I need this preg_match to check on on A-Za-z + space. I think in this case it doesn't matter if it's matching more than 1 spaces because it's replaced anyway, right?

EDIT:

Solution for my case:

$usersurname = preg_replace("/\s{2,}/"," ", $usersurname);
if (!preg_match("/[^A-Za-z ]/", $usersurname))

This does the work. Thanks for helping out, J0HN

  • 写回答

1条回答 默认 最新

  • duanqu9279 2012-11-20 11:07
    关注

    Well, solving the problem you have in mind:

    if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usersurname)) { ... }
    

    But, well, it's just a part of the solution, and it's not bulletproof. Look at the list of common mistakes when handling names.

    So, you'd better to re-think on your validation approach.

    Replacing the multiple spaces is simpler to achieve as a separate instruction, something like

    $processed_usersurname = preg_replace("/\s{2,}/"," ", $usersurname); 
    

    This will match and replace any two or more consequent whitespace characters (space, tab, linebreak and carriage return) to single space

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘