Sar*_*raz 27
你可以过滤它:
$text = preg_replace("/[^a-zA-Z0-9]+/", "", $text);
Run Code Online (Sandbox Code Playgroud)
至于某些符号,您应该更具体
Ser*_* S. 26
您可以$str使用preg_match以下方法测试字符串(let ):
if(preg_match("/^[a-zA-Z0-9]+$/", $str) == 1) {
// string only contain the a to z , A to Z, 0 to 9
}
Run Code Online (Sandbox Code Playgroud)
如果您需要更多符号,可以先添加它们 ]
Gui*_*nto 11
不需要正则表达式,可以使用以下Ctype功能:
ctype_alnum:检查字母数字字符ctype_alpha:检查字母字符ctype_cntrl:检查控制字符ctype_digit:检查数字字符ctype_graph:检查除空格外的任何可打印字符ctype_lower:检查小写字符ctype_print:检查可打印字符ctype_punct:检查任何不是空格或字母数字字符的可打印字符ctype_space:检查空白字符ctype_upper:检查大写字符ctype_xdigit:检查表示十六进制数字的字符在你的情况下使用ctype_alnum,例如:
if (ctype_alnum($str)) {
//...
}
Run Code Online (Sandbox Code Playgroud)
例:
<?php
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
if (ctype_alnum($testcase)) {
echo 'The string ', $testcase, ' consists of all letters or digits.';
} else {
echo 'The string ', $testcase, ' don\'t consists of all letters or digits.';
}
}
Run Code Online (Sandbox Code Playgroud)
在线示例:https://ideone.com/BYN2Gn