Home  >  Article  >  Backend Development  >  求教1简单正则?回帖给分

求教1简单正则?回帖给分

WBOY
WBOYOriginal
2016-06-13 13:08:01827browse

求教一简单正则?回帖给分!
电话号码字段(tel varchar类型)存储3个号码,并且是以分号结尾,类似:
0510-12323475,0781-26354589,021-25698746 (同时存储3个号码)
0521-25698745,, (有可能只存储1个电话号码)
0521-25698745,021-25698475, (有可能只存储2个电话号码)
现在用正则判断电话号码输入是否正常,注意电话区号。
RegTel=/^(\d{3,4}-\d{7,8}(-\d{1,4})?)?$/这是我写的,但是不行。

------解决方案--------------------

PHP code

$str = '0510-12323475,,';
$pattern = '/,?\d{4}-\d+,?/';

var_dump(preg_match($pattern, $str));
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code
$ar = array(
 '0510-12323475,0781-26354589,021-25698746',
 '0521-25698745,,',
 '0521-25698745,021-25698475',
 '0317-88641,ttttttt',
);

$pattern = '/(?:\d{3,}-\d+,|,){3}/';

foreach($ar as $v) {
  $v = trim($v) . str_repeat(',', 3-substr_count($v, ','));//补齐逗号
  echo $v . ' ' . preg_match($pattern, $v) , PHP_EOL;
}
<br><font color="#e78608">------解决方案--------------------</font><br>
探讨
老大,我只要$pattern的正则。但是你的$pattern不行。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn