简体   繁体   English

PHP preg_match 仅特殊字符

[英]PHP preg_match special characters only

I try to find a preg_match that filters strings that contain only special characters like - .我试图找到一个 preg_match 来过滤包含像-这样的特殊字符的字符串。

However, the string itself can contain special characters but it should not be special characters only.但是,字符串本身可以包含特殊字符,但不应只是特殊字符。

Any ideas how to do that?任何想法如何做到这一点?

Result should return true if string only contains special characters.如果字符串仅包含特殊字符,则结果应返回 true。 So something like所以像

if(preg_match('//',$string)) echo $string; //I leave the pattern empty as this is the actual question.

You need to match the string for any alphanumeric character.您需要匹配任何字母数字字符的字符串。

$one=  "%^&";
$two = "asd%asd";


function notOnlySpecialChars($str) {
    if (preg_match('/[A-Za-z0-9]/', $str)) { //Replaced _ with -
        return true;
    } else {
        return false;
    }
}
notOnlySpecialChars($one); //false
notOnlySpecialChars($two); //true

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

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