tags:

views:

35

answers:

4
+1  Q: 

php regex problem

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

Hi, my problem is that this isn't working as it should.

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

they always start with: STEAM_0:

Two true examples would be: 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.

I hope you can help me because I have little knowledge in this, thnkas in advance

+2  A: 

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:

$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

$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";
    }
}
?>
The MYYN
Replacing `(0|1)` with `[01]` would prevent capturing, and therefore improve performance.
Tim
it doesnt work, the STEAM ID is just like this: STEAM_0:1:2796878
Afonso
if you can make this work for the steamid that I posted above it works for everyother attending the conditions in my original post
Afonso
@afonso: corrected the 01 post-second-colon part in my post ...
The MYYN
A: 

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:

$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

ArtBIT
I just found out that something in the code was wrong, if you notice in my original post there ssteamid insted of steamid, I corrected it but still with your regex it isnt working, am I using thw wrong function? instead of ereg should I be using something else?
Afonso
Yeah, `eregi` is deprecated, you can achieve the same thing with `preg_match`. See http://www.php.net/manual/en/function.preg-match.php for usage.
ArtBIT
You can write `[0-9]{7}[0-9]?` as `[0-9]{7,8}`.
Gumbo
@Gumbo, yup thanks, I honestly don't know why I went for this approach.
ArtBIT
A: 

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
  • Either 8 or 9 digits, making the total number of characters 17 or 18
codaddict
A: 

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).

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.

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:

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

Hope that helps.

Spudley
Thanks you all , I think all your regex work, the thing is I was using ereg, thanks for your explanations :) it works now
Afonso
If the advice has helped you, please "accept" one of the answers by clicking the tick icon next to it. :-)
Spudley