将字符串验证为具有某些特殊字符的字母数字

Validate a string as alphanumeric with some special character

我检查了这个 link Validate username as alphanumeric with underscores.

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

我的条件;

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

如何在php帮助

中做到这一点

编辑:

试试

它显示语法错误:

<?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.";
}
?>

只需添加它们:

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

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

任何标记的都是合法的。请注意,这也匹配 space。如果那是一个错误,只需删除 space.