将字符串验证为带有一些特殊字符的字母数字

时间:2015-09-09 08:39:41

标签: php regex string preg-match

我已查看此链接Validate username as alphanumeric with underscores

我想在字符串检查中添加更多条件

我的条件;

条件1: Alphanumeric including ! "#$%&'()*+,-./:;=?@^_~

如何在php帮助中做到这一点

编辑:

尝试使用melwil answer

显示语法错误:

<?php

if (preg_match("//^[A-Za-z0-9_! "#$%&'()*+,\-.\\:\/;=?@^_]+$/", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

1 个答案:

答案 0 :(得分:4)

只需添加它们:

preg_match("/^[A-Za-z0-9_! \"#$%&'()*+,\-.\\:\/;=?@^_]+$/", "PHP is the web scripting language of choice.")

演示:https://regex101.com/r/sT9lG9/1

任何标记都是合法的。请注意,这也符合空间。如果这是一个错误,只需删除空格。