简体   繁体   English

如何验证一个字符串,可以包含数字空格,+和 - 使用正则表达式php

[英]How to validate a string which can contain numbers spaces, + and - using regex php

I want to validate phone numbers like follows using php. 我想使用php验证电话号码,如下所示。

+009411774132482 011 2560-205 +009411774132482 011 2560-205

试试这个'/^\\+?[0-9 \\-]+$/'

This should do it... 这应该做到......

preg_match('/^\+?[\d -]+\z/', $str);

CodePad . CodePad

It will allow... 它会允许......

  • An optional preceding + . 一个可选的前面的+
  • More than one of digits ( 0-9 ), space and - . 多个数字( 0-9 ),空格和- If you want a minimum amount, swap the + with {9,} . 如果您想要最低金额,请将+{9,}交换。
  • Does not allow a trailing \\n . 不允许结尾\\n

试试这个正则表达式:

^[\d\s+-]+$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM