PHP正则表达式,用于字符串中最多两个特殊字符

时间:2014-12-10 18:35:18

标签: php validation

我正在验证PHP中的文本字段,其中我允许使用字母,数字,点,短划线和下划线。但我想要最多两个点和/或两个破折号和/或两个下划线。我该怎么办?如果这不可行,那么我怎样才能允许上述任何两个中的两个?

1 个答案:

答案 0 :(得分:0)

我会这样做。

function checkString($string){

    $c1 = substr_count($string, '.');
    $c2 = substr_count($string, '-');
    $c3 = substr_count($string, '_');

    if($c1 <= 2 && $c2 <= 2 && $c3 <= 2){
        return true;
    }else{
        return false;
    }

}