【问题标题】:Validate a string as alphanumeric with some special character使用一些特殊字符将字符串验证为字母数字
【发布时间】:2015-12-05 03:26:40
【问题描述】:

我已经检查了这个链接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 和条件 2 之间的区别。
  • @anubhava,请添加答案
  • 为什么要投反对票。我无法理解那些以任何理由投反对票的人
  • 你可以使用这个正则表达式:'/^[A-Za-z0-9_! "#$%&amp;\'()*+,.:\/;=?@^-]+$/'

标签: php regex string preg-match


【解决方案1】:

只需添加它们:

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

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

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

【讨论】:

    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    相关资源
    最近更新 更多