Php 仅允许字符串中的某些字符
我一直在寻找一种方法来创建一个函数来检查字符串中是否包含除小写字母和数字以外的任何内容,以及它是否返回false。我已经在互联网上搜索过了,但我能找到的只是一些旧方法,它们要求您使用PHP5中现在不推荐使用的函数。来混合一些东西Php 仅允许字符串中的某些字符,php,string,function,filter,Php,String,Function,Filter,我一直在寻找一种方法来创建一个函数来检查字符串中是否包含除小写字母和数字以外的任何内容,以及它是否返回false。我已经在互联网上搜索过了,但我能找到的只是一些旧方法,它们要求您使用PHP5中现在不推荐使用的函数。来混合一些东西 <? $input = "hello world 123!"; $digits = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0"); if (ctype_alnum($input)) { i
<?
$input = "hello world 123!";
$digits = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
if (ctype_alnum($input))
{
if (ctype_lower(str_replace($digits, "", $input)))
{
// Input is only lowercase and digits
}
}
?>
但正则表达式可能是实现这一点的方法!=) 使用正则表达式。使用
因此,如果
$matches
具有1
,您就知道$string
包含错误字符。否则,$matches
为0
,$string
正常。请记住,此函数也可以返回false,因此您应该使用类型比较运算符,===
:$matches=preg_match('/[^a-z0-9]/',$string)检查它;如果($matches==0){//匹配时在此处编码}
function check_input( $text ) {
if( preg_match( "/[^a-z0-9]/", $text ) ) {
return false;
}
else {
return true;
}
}
function check_input( $text ) {
if( preg_match( "/[^a-z0-9]/", $text ) ) {
return false;
}
else {
return true;
}
}