简体   繁体   English

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

[英]PHP Regular expression for maximum of two special characters in a string

I am validating a text field in PHP in which I allow letters, numbers, dots, dashes, and underscore. 我正在验证PHP中的文本字段,在其中允许字母,数字,点,破折号和下划线。 But I want a maximum of two dots and/or two dashes and/or two underscores. 但是我最多需要两个点和/或两个破折号和/或两个下划线。 How would I do that? 我该怎么做? And if that's not doable, then how would I allow maximum of two of any of the above? 如果这不可行,那么我如何允许上述任何两个中的最多两个?

I would do it this way. 我会这样做。

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;
    }

}

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

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