【发布时间】:2009-09-25 19:24:45
【问题描述】:
值将采用我希望 preg_match 工作的格式 123-123-123-12345。你能看出这个正则表达式有什么问题吗?
foreach($elem as $key=>$value) {
// Have tried this With and without the + before the $ as well
if(preg_match("/^[0-9]{3}\-[0-9]{3}\-[0-9]{3}\-[0-9]{5}+$/", $value)) {
echo "Yeah match Elm: ".$value."<br />";
} else {
echo "Boo Hoo Elm: '".$value."'<br />";
}
}
也试过
/^\d{3}\-\d{3}\-\d{3}\-\d{5}+$/
//With and without the + before the $
Boo Hoo 都失败了 ;(
编辑:
var_dump($elem)
array(3) { [0]=> string(1) "c" [1]=> string(0) "" [2]=> string(36) "123-123-123-12345" }
【问题讨论】:
-
在这种情况下,您无需转义破折号。
-
+ 似乎什么也没做,不管有没有它,它都可以工作,最后会失败,有 10 位数字。
-
顺便说一句,您的数据有些奇怪,我只数了 17 个字符,为什么要给出一个字符串 (36)?
标签: php regex arrays preg-match