php regex表示最小和最大数量长度

时间:2021-01-28 11:51:55
if(strlen(trim($steamid)) != 0)
            {
                $regex = "/^STEAM_0:(0|1):[0-9]{1}[0-9]{0,8}$/";
                if(!ereg($regex, $ssteamid)){
                    echo "STEAM ID invalida";
                }
            }

My problem is that this isn't working as it should.

我的问题是,这并没有发挥应有的作用。

STEAM ID's have maximum of 18 characters and minimum of 17.

蒸汽ID的最大字符数为18,最小字符数为17。

they always start with: STEAM_0:

它们总是以:STEAM_0开头:

Two true examples would be: STEAM_0:0:11111111 ; STEAM_0:1:1111111

两个真正的例子是:STEAM_0:0:11111111;STEAM_0:1:1111111

And another thing is that after STEAM_0: always come an 0 or an 1 like demonstrated in the examples.

另一件事是,在蒸汽之后:总是在例子中显示0或1。

4 个解决方案

#1


3  

This:

这样的:

$regex = "/^STEAM_0:(0|1):[0-9]{1}[0-9]{0,8}$/";

could be writter shorter as:

可以缩短为:

$regex = "/^STEAM_0:[01]:[0-9]{1,9}$/";

Since your ID is 17 or 18 chars long, adjust the regex to it:

由于您的ID长度为17或18字符,请将regex调整为:

$regex = "/^STEAM_0:[01]:[0-9]{7,8}$/";

Finally, note that ereg is deprecated as of PHP 5.3. This snippet shows your php/regex with preg_match:

最后,请注意,在PHP 5.3中不赞成使用ereg。这个片段显示了您的php/regex和preg_match:

<?php

$steamid = "STEAM_0:0:11111111";

if(strlen(trim($steamid)) != 0)
{
    $regex = "/^STEAM_0:[01]:[0-9]{7,8}$/";
    if(!preg_match($regex, $steamid))
    {
        echo "STEAM ID invalida.\n";
    } else {
        echo "STEAM ID valida.\n";
    }
}
?>

#2


0  

If the minimum length is 17 and the max length is 18, then you guys are looking at this at the wrong way. This should do the job for you:

如果最小长度是17,最大长度是18,那么你们就看错了。这应该对你有帮助:

$regex = "/^STEAM_0:(0|1):[0-9]{7}[0-9]?$/";

And also, when in doubt, test your regexp with some of the online regexp tools like http://www.regexplanet.com/simple/index.html

此外,如果有疑问,还可以使用http://www.reg解释器/simple/index.html等在线regexp工具对regexp进行测试

#3


0  

Try this:

试试这个:

$regex = "/^STEAM_0:(0|1):[0-9]{8,9}$/";

It matches:

它匹配:

  • STEAM_0: at the beginning, followed by
  • 蒸汽:开始,然后
  • a 0 or a 1, followed by a :, followed by
  • 0或1,然后是a:后面跟着。
  • Either 8 or 9 digits, making the total number of characters 17 or 18
  • 8位或9位,使字符总数为17或18

#4


0  

You should be avoiding use of ereg() and its related functions; they've deprecated for quite a long time in PHP. Use thepreg_xx() functions instead (preg_match() in this case).

您应该避免使用ereg()及其相关函数;在PHP中,它们已经被弃用了很长时间。使用preg_xx()函数代替(在本例中是preg_match()))。

One of the differences between preg_match() and ereg() is that ereg() does not expect you to include the slash characters to start and end the regex, whereas preg_match() does. This is probably the main reason your code isn't working: since you've included the slashes, your use of ereg() won't work, but is you switch to preg_match(), the same expression should work fine.

preg_match()和ereg()之间的一个区别是,ereg()不希望包含开始和结束regex的斜线字符,而preg_match()则希望包含。这可能是代码不能正常工作的主要原因:既然包含了斜线,那么使用ereg()将不起作用,但是如果您切换到preg_match(),那么同样的表达式应该可以正常工作。

So a simple change to preg_match() should sort you problem. But while I'm here, I may also suggest that you can shorten your regex string quite considerably. Try something like this:

因此,对preg_match()的一个简单更改应该会对您的问题进行排序。但是当我在这里的时候,我可能也会建议您可以大幅缩短regex字符串。试试这样:

$regex = "/^STEAM_0:[01]:[0-9]{8,9}$/";
if(!preg_match($regex, $ssteamid)) {
    echo "STEAM ID invalid";
}

Hope that helps.

希望有帮助。

#1


3  

This:

这样的:

$regex = "/^STEAM_0:(0|1):[0-9]{1}[0-9]{0,8}$/";

could be writter shorter as:

可以缩短为:

$regex = "/^STEAM_0:[01]:[0-9]{1,9}$/";

Since your ID is 17 or 18 chars long, adjust the regex to it:

由于您的ID长度为17或18字符,请将regex调整为:

$regex = "/^STEAM_0:[01]:[0-9]{7,8}$/";

Finally, note that ereg is deprecated as of PHP 5.3. This snippet shows your php/regex with preg_match:

最后,请注意,在PHP 5.3中不赞成使用ereg。这个片段显示了您的php/regex和preg_match:

<?php

$steamid = "STEAM_0:0:11111111";

if(strlen(trim($steamid)) != 0)
{
    $regex = "/^STEAM_0:[01]:[0-9]{7,8}$/";
    if(!preg_match($regex, $steamid))
    {
        echo "STEAM ID invalida.\n";
    } else {
        echo "STEAM ID valida.\n";
    }
}
?>

#2


0  

If the minimum length is 17 and the max length is 18, then you guys are looking at this at the wrong way. This should do the job for you:

如果最小长度是17,最大长度是18,那么你们就看错了。这应该对你有帮助:

$regex = "/^STEAM_0:(0|1):[0-9]{7}[0-9]?$/";

And also, when in doubt, test your regexp with some of the online regexp tools like http://www.regexplanet.com/simple/index.html

此外,如果有疑问,还可以使用http://www.reg解释器/simple/index.html等在线regexp工具对regexp进行测试

#3


0  

Try this:

试试这个:

$regex = "/^STEAM_0:(0|1):[0-9]{8,9}$/";

It matches:

它匹配:

  • STEAM_0: at the beginning, followed by
  • 蒸汽:开始,然后
  • a 0 or a 1, followed by a :, followed by
  • 0或1,然后是a:后面跟着。
  • Either 8 or 9 digits, making the total number of characters 17 or 18
  • 8位或9位,使字符总数为17或18

#4


0  

You should be avoiding use of ereg() and its related functions; they've deprecated for quite a long time in PHP. Use thepreg_xx() functions instead (preg_match() in this case).

您应该避免使用ereg()及其相关函数;在PHP中,它们已经被弃用了很长时间。使用preg_xx()函数代替(在本例中是preg_match()))。

One of the differences between preg_match() and ereg() is that ereg() does not expect you to include the slash characters to start and end the regex, whereas preg_match() does. This is probably the main reason your code isn't working: since you've included the slashes, your use of ereg() won't work, but is you switch to preg_match(), the same expression should work fine.

preg_match()和ereg()之间的一个区别是,ereg()不希望包含开始和结束regex的斜线字符,而preg_match()则希望包含。这可能是代码不能正常工作的主要原因:既然包含了斜线,那么使用ereg()将不起作用,但是如果您切换到preg_match(),那么同样的表达式应该可以正常工作。

So a simple change to preg_match() should sort you problem. But while I'm here, I may also suggest that you can shorten your regex string quite considerably. Try something like this:

因此,对preg_match()的一个简单更改应该会对您的问题进行排序。但是当我在这里的时候,我可能也会建议您可以大幅缩短regex字符串。试试这样:

$regex = "/^STEAM_0:[01]:[0-9]{8,9}$/";
if(!preg_match($regex, $ssteamid)) {
    echo "STEAM ID invalid";
}

Hope that helps.

希望有帮助。