[Solved] Regex Advice Needed!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

[Solved] Regex Advice Needed!

Post 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..
Last edited by SystemWisdom on Fri Apr 01, 2005 2:50 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post 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!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post by SystemWisdom »

Ahh.. okay!! Thx!
Post Reply