使用PHP只允许[a-z][a-z][0-9]。

时间:2023-02-11 03:50:08

How can I get a string that only contains a to z, A to Z, 0 to 9 and some symbols?

我怎样才能得到一个只包含a到z, a到z, 0到9和一些符号的字符串呢?

7 个解决方案

#1


24  

You can test your string (let $str) using preg_match:

您可以使用preg_match测试您的字符串(让$str):

if(preg_match("/^[a-zA-Z0-9]+$/", $str) == 1) {
    // string only contain the a to z , A to Z, 0 to 9
}

If you need more symbols you can add them before ]

如果你需要更多的符号,你可以添加它们

#2


23  

You can filter it like:

你可以这样过滤:

$text = preg_replace("/[^a-zA-Z0-9]+/", "", $text);

As for some symbols, you should be more specific

对于一些符号,你应该更具体一些。

#3


7  

Don't need regex, you can use the Ctype functions:

不需要regex,可以使用Ctype函数:

  • ctype_alnum: Check for alphanumeric character(s)
  • ctype_alnum:检查字母数字字符(s)
  • ctype_alpha: Check for alphabetic character(s)
  • ctype_alpha:检查字母字符(s)
  • ctype_cntrl: Check for control character(s)
  • ctype_cntrl:检查控制字符(s)
  • ctype_digit: Check for numeric character(s)
  • ctype_digit:检查数字字符(s)
  • ctype_graph: Check for any printable character(s) except space
  • ctype_graph:检查除空格外的任何可打印字符。
  • ctype_lower: Check for lowercase character(s)
  • ctype_lower:检查小写字符(s)
  • ctype_print: Check for printable character(s)
  • ctype_print:检查可打印字符
  • ctype_punct: Check for any printable character which is not whitespace or an alphanumeric character
  • ctype_punct:检查没有空格或字母数字字符的可打印字符。
  • ctype_space: Check for whitespace character(s)
  • ctype_space:检查空白字符(s)
  • ctype_upper: Check for uppercase character(s)
  • ctype_upper:检查大写字符(s)
  • ctype_xdigit: Check for character(s) representing a hexadecimal digit
  • ctype_xdigit:检查表示十六进制数字的字符。

In your case use ctype_alnum, example:

在您的案例中使用ctype_alnum,示例:

if (ctype_alnum($str)) {
    //...
}

Example:

例子:

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

    }
}

Online example: https://ideone.com/BYN2Gn

网上的例子:https://ideone.com/BYN2Gn

#4


2  

Why are those "symbols" in there in the first place? Looks to me like you're reading the text from a source that's encoded as UTF-16, but you're decoding it as something else, such as latin1 or ASCII.

为什么一开始就有这些“符号”呢?在我看来,你正在从一个编码为UTF-16的源读取文本,但是你将它解码为其他东西,比如latin1或ASCII。

#5


1  

Both these regexes should do it:

这两个regex都应该这样做:

$str = preg_replace('~[^a-z0-9]+~i', '', $str);

Or:

或者:

$str = preg_replace('~[^a-zA-Z0-9]+~', '', $str);

#6


0  

The best and most flexible way to accomplish that is using regular expressions. But I`m not sure how to do that in PHP but this article can help. link

实现这一目标的最佳和最灵活的方法是使用正则表达式。但是我不知道如何在PHP中做到这一点,但本文可以提供帮助。链接

#7


0  

A shortcut will be as below also:

快捷方式如下:

if (preg_match('/^[\w\.]+$/', $str)) {
    echo 'Str is valid and allowed';
} else
    echo 'Str is invalid';

Here:

在这里:

// string only contain the a to z , A to Z, 0 to 9 and _ (underscore)
\w - matches [a-zA-Z0-9_]+

Hope it helps!

希望它可以帮助!

#1


24  

You can test your string (let $str) using preg_match:

您可以使用preg_match测试您的字符串(让$str):

if(preg_match("/^[a-zA-Z0-9]+$/", $str) == 1) {
    // string only contain the a to z , A to Z, 0 to 9
}

If you need more symbols you can add them before ]

如果你需要更多的符号,你可以添加它们

#2


23  

You can filter it like:

你可以这样过滤:

$text = preg_replace("/[^a-zA-Z0-9]+/", "", $text);

As for some symbols, you should be more specific

对于一些符号,你应该更具体一些。

#3


7  

Don't need regex, you can use the Ctype functions:

不需要regex,可以使用Ctype函数:

  • ctype_alnum: Check for alphanumeric character(s)
  • ctype_alnum:检查字母数字字符(s)
  • ctype_alpha: Check for alphabetic character(s)
  • ctype_alpha:检查字母字符(s)
  • ctype_cntrl: Check for control character(s)
  • ctype_cntrl:检查控制字符(s)
  • ctype_digit: Check for numeric character(s)
  • ctype_digit:检查数字字符(s)
  • ctype_graph: Check for any printable character(s) except space
  • ctype_graph:检查除空格外的任何可打印字符。
  • ctype_lower: Check for lowercase character(s)
  • ctype_lower:检查小写字符(s)
  • ctype_print: Check for printable character(s)
  • ctype_print:检查可打印字符
  • ctype_punct: Check for any printable character which is not whitespace or an alphanumeric character
  • ctype_punct:检查没有空格或字母数字字符的可打印字符。
  • ctype_space: Check for whitespace character(s)
  • ctype_space:检查空白字符(s)
  • ctype_upper: Check for uppercase character(s)
  • ctype_upper:检查大写字符(s)
  • ctype_xdigit: Check for character(s) representing a hexadecimal digit
  • ctype_xdigit:检查表示十六进制数字的字符。

In your case use ctype_alnum, example:

在您的案例中使用ctype_alnum,示例:

if (ctype_alnum($str)) {
    //...
}

Example:

例子:

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

    }
}

Online example: https://ideone.com/BYN2Gn

网上的例子:https://ideone.com/BYN2Gn

#4


2  

Why are those "symbols" in there in the first place? Looks to me like you're reading the text from a source that's encoded as UTF-16, but you're decoding it as something else, such as latin1 or ASCII.

为什么一开始就有这些“符号”呢?在我看来,你正在从一个编码为UTF-16的源读取文本,但是你将它解码为其他东西,比如latin1或ASCII。

#5


1  

Both these regexes should do it:

这两个regex都应该这样做:

$str = preg_replace('~[^a-z0-9]+~i', '', $str);

Or:

或者:

$str = preg_replace('~[^a-zA-Z0-9]+~', '', $str);

#6


0  

The best and most flexible way to accomplish that is using regular expressions. But I`m not sure how to do that in PHP but this article can help. link

实现这一目标的最佳和最灵活的方法是使用正则表达式。但是我不知道如何在PHP中做到这一点,但本文可以提供帮助。链接

#7


0  

A shortcut will be as below also:

快捷方式如下:

if (preg_match('/^[\w\.]+$/', $str)) {
    echo 'Str is valid and allowed';
} else
    echo 'Str is invalid';

Here:

在这里:

// string only contain the a to z , A to Z, 0 to 9 and _ (underscore)
\w - matches [a-zA-Z0-9_]+

Hope it helps!

希望它可以帮助!