繁体   English   中英

PHP preg_match 仅用于数字和字母,无特殊字符

[英]PHP preg_match for only numbers and letters, no special characters

我不想要 preg_match_all ......因为表单字段只允许数字和字母......只是想知道正确的语法是什么......

没什么特别的……只需要知道只查找数字和字母的 preg_match 语句的正确语法。 就像是

preg_match('/^([^.]+)\.([^.]+)\.com$/', $unit)

但这也不是寻找数字......

如果您只想确保字符串仅包含字母数字字符。 AZ, az, 0-9 不需要使用正则表达式。

使用ctype_alnum()

文档中的示例:

<?php
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
    if (ctype_alnum($testcase)) {
        echo "The string $testcase consists of all letters or digits.\n";
    } else {
        echo "The string $testcase does not consist of all letters or digits.\n";
    }
}
?>

上面的例子将输出:

The string AbCd1zyZ9 consists of all letters or digits.
The string foo!#$bar does not consist of all letters or digits.
if(preg_match("/[A-Za-z0-9]+/", $content) == TRUE){

} else {

}

如果您想匹配 1 个以上,那么您需要向我们提供一些代码,我们可以提供更好的帮助。

虽然,与此同时:

preg_match("/([a-zA-Z0-9])/", $formContent, $result);
print_r($result);

:)

暂无
暂无

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

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