Detecting special characters PHP

后端 未结 2 737
旧巷少年郎
旧巷少年郎 2021-01-25 16:28

I am trying to check whether a string contains any special chars so I know what to do with it afterwards in my script.

Heres what I have:

if (preg_match(         


        
2条回答
  •  盖世英雄少女心
    2021-01-25 17:11

    You could compare all alphanumeric characters against the string to check if there is any special character:

    $temp = '$tack0verflow_';
    
    if(strlen($temp) == strlen(preg_replace("/[^a-zA-Z0-9]+/", "", $temp))){
        // No special characters
    } else {
        // Has special characters
    }
    

提交回复
热议问题