PHP preg_match - 只允许字母数字字符串和 - _ 字符

发布于 2024-12-09 18:42:45 字数 237 浏览 0 评论 0原文

我需要正则表达式来检查字符串是否只包含数字、字母、连字符或下划线

$string1 = "This is a string*";
$string2 = "this_is-a-string";

if(preg_match('******', $string1){
   echo "String 1 not acceptable acceptable";
   // String2 acceptable
}

I need the regex to check if a string only contains numbers, letters, hyphens or underscore

$string1 = "This is a string*";
$string2 = "this_is-a-string";

if(preg_match('******', $string1){
   echo "String 1 not acceptable acceptable";
   // String2 acceptable
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

要走干脆点 2024-12-16 18:42:45

代码:

if(preg_match('/[^a-z_\-0-9]/i', $string))
{
  echo "not valid string";
}

解释:

  • [] =>字符类定义
  • ^ =>否定 a-z 类
  • =>从 'a' 到 'z' 的字符
  • _ =>下划线
  • -=>连字符“-”(您需要转义它)
  • 0-9 =>数字(从零到九)

正则表达式末尾的“i”修饰符用于“不区分大小写”,如果您不输入,则需要在执行 AZ 之前在代码中添加大写字符

Code:

if(preg_match('/[^a-z_\-0-9]/i', $string))
{
  echo "not valid string";
}

Explanation:

  • [] => character class definition
  • ^ => negate the class
  • a-z => chars from 'a' to 'z'
  • _ => underscore
  • - => hyphen '-' (You need to escape it)
  • 0-9 => numbers (from zero to nine)

The 'i' modifier at the end of the regex is for 'case-insensitive' if you don't put that you will need to add the upper case characters in the code before by doing A-Z

凉宸 2024-12-16 18:42:45
if(!preg_match('/^[\w-]+$/', $string1)) {
   echo "String 1 not acceptable acceptable";
   // String2 acceptable
}
if(!preg_match('/^[\w-]+$/', $string1)) {
   echo "String 1 not acceptable acceptable";
   // String2 acceptable
}
终止放荡 2024-12-16 18:42:45

这是 UTF-8 世界公认的答案的一个等价物。

if (!preg_match('/^[\p{L}\p{N}_-]+$/u', $string)){
  //Disallowed Character In $string
}

解释:

  • [] =>字符类定义
  • p{L} =>匹配任何语言的任何类型的字母字符
  • p{N} =>匹配任何类型的数字字符
  • _- =>匹配下划线和连字符
  • + =>量词 — 匹配 1 次到无限次(贪婪)
  • /u =>统一码修饰符。模式字符串被视为 UTF-16。还
    导致转义序列匹配 unicode 字符

请注意,如果连字符是类定义中的最后一个字符,则它不需要转义。如果破折号出现在类定义中的其他位置,则它需要转义,因为它将被视为范围字符而不是连字符。

Here is one equivalent of the accepted answer for the UTF-8 world.

if (!preg_match('/^[\p{L}\p{N}_-]+$/u', $string)){
  //Disallowed Character In $string
}

Explanation:

  • [] => character class definition
  • p{L} => matches any kind of letter character from any language
  • p{N} => matches any kind of numeric character
  • _- => matches underscore and hyphen
  • + => Quantifier — Matches between one to unlimited times (greedy)
  • /u => Unicode modifier. Pattern strings are treated as UTF-16. Also
    causes escape sequences to match unicode characters

Note, that if the hyphen is the last character in the class definition it does not need to be escaped. If the dash appears elsewhere in the class definition it needs to be escaped, as it will be seen as a range character rather then a hyphen.

伪装你 2024-12-16 18:42:45

\w\- 可能是最好的,但这里只是另一种选择
使用 [:alnum:]

if(!preg_match("/[^[:alnum:]\-_]/",$str)) echo "valid";

demo1 | 演示2

\w\- is probably the best but here just another alternative
Use [:alnum:]

if(!preg_match("/[^[:alnum:]\-_]/",$str)) echo "valid";

demo1 | demo2

过度放纵 2024-12-16 18:42:45

为什么要使用正则表达式? PHP 有一些内置功能可以执行

<?php
    $valid_symbols = array('-', '_');
    $string1 = "This is a string*";
    $string2 = "this_is-a-string";

    if(preg_match('/\s/',$string1) || !ctype_alnum(str_replace($valid_symbols, '', $string1))) {
        echo "String 1 not acceptable acceptable";
    }
?>

preg_match('/\s/',$username) 检查空格

!ctype_alnum(str_replace($valid_symbols, '', $string1) ) 将检查 valid_symbols

Why to use regex? PHP has some built in functionality to do that

<?php
    $valid_symbols = array('-', '_');
    $string1 = "This is a string*";
    $string2 = "this_is-a-string";

    if(preg_match('/\s/',$string1) || !ctype_alnum(str_replace($valid_symbols, '', $string1))) {
        echo "String 1 not acceptable acceptable";
    }
?>

preg_match('/\s/',$username) will check for blank space

!ctype_alnum(str_replace($valid_symbols, '', $string1)) will check for valid_symbols

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文