Page 1 of 1

[Solved] Regex Advice Needed!

Posted: Fri Apr 01, 2005 2:09 pm
by SystemWisdom
I am very new to Regex's and I have a very basic regex that I want to make sure is correct, so any help is appreciated..

What I had originally (not a regex) was:
(Note: $aLen is an array of 2: Min & Max Allowed Length)

Code: Select all

function ValidateInput( $szInput, $aLen = 0 )
{   global $Cfg_InputMinStd, $Cfg_InputMaxStd;

    // Determine Valid Length
    if( !is_array( $aLen ) ) $aLen = array( $Cfg_InputMinStd, $Cfg_InputMaxStd );

    // Check For Valid Length
    if( strlen( $szInput ) < $aLen[0] || strlen( $szInput ) > $aLen[1] )
        return false;

    $szValidChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';

    // Check Input For Valid Characters
    if( strspn( $szInput, $szValidChars ) != strlen( $szInput ) )
        return false;

    // All Went Well, Input Contains Valid Data
    return true;
}
But I have recently changed it to this:

Code: Select all

function ValidateInput( $szInput, $aLen = 0 )
{   global $Cfg_InputMinStd, $Cfg_InputMaxStd;

    // Determine Valid Length
    if( !is_array( $aLen ) ) $aLen = array( $Cfg_InputMinStd, $Cfg_InputMaxStd );

    return (preg_match( '/\w{'.$aLen[0].','.$aLen[1].'}/', $szInput )) ? true : false;
}
It is a very basic pattern, but I wanted to know if the newer function is doing the exact same thing as the old, and if not what is wrong?

I have tested it and it seems to work, but there are so many possibilities I thought it would be easier to ask here..

Posted: Fri Apr 01, 2005 2:30 pm
by feyd
I don't think \w covers all the characters you are wanting to match.. plus you have some pattern issues..

Code: Select all

return (bool) preg_match( '#^&#1111;a-zA-Z0-9_]{' . $aLen&#1111;0] . ',' . $alen&#1111;1] . '}$#', $szInput);
Used to Hungarian I see. :)

Posted: Fri Apr 01, 2005 2:37 pm
by SystemWisdom
Ahh, I see, that makes sense too!! Thx!

Only, I dont understand the # u used, what is that for?? Or is it just an alternative to the / I used (denoting the start/end of the pattern)??

Yes, very used to Hungarian, and I forgot about Explicit Type-Casting in PHP.. :oops:

:D Thx again!!

Posted: Fri Apr 01, 2005 2:43 pm
by feyd
# is the character I use for start and end markers.. because a lot of patterns I deal with involve a forward slash or six.. escaping all of them gets pretty annoying. :)

Posted: Fri Apr 01, 2005 2:50 pm
by SystemWisdom
Ahh.. okay!! Thx!