[Solved] Regex Advice Needed!
Posted: Fri Apr 01, 2005 2:09 pm
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)
But I have recently changed it to this:
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..
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;
}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;
}I have tested it and it seems to work, but there are so many possibilities I thought it would be easier to ask here..