Link to home
Start Free TrialLog in
Avatar of huang_ck
huang_ck

asked on

php how to check a GET variable contains a substring or not

Hi guys,

I would like to know how to check if the a GET variable contains a string pattern, eg:

http://xxx.com/index.php?id=abcdefg

I want a script so that it can check if the 'id' is in the pattern 'abcd'
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, should be !==

-r-
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of swymer
swymer

VoteyDisciple is correct that a regular expression is a much more robust solution and to match your request, without having more detail from you on exactly what you are looking for, some possible regular expression strings you might use (from most specific to most general) are:

$regex = '/^abcd[a-z]{1,3}/';  <-- This checks for 'abcd' then at least 1 but up to 3 additional lower-case ONLY word characters
$regex = '/^abcd[a-z]+/';  <-- This checks for 'abcd' then at least 1 additional lower-case ONLY word characters
$regex = '/^abcd[a-z]*/';  <-- This checks for 'abcd' then possibley other additional lower-case ONLY word characters
$regex = '/^[a-z]{4,7}/';  <-- This checks for at least 4 but up to 7 lower-case ONLY word characters
$regex = '/^[a-z]{4,}/';  <-- This checks for 4 or more word lower-case ONLY word characters
$regex = '/^\w{4,}/';  <-- This checks for 4 or more of any type of word characters { a-z,A-Z,0-9,_ }

(MANY other possible combinations are possible.  The standard reference for PERL regular expressions, which is what you are using if you use the "preg_..." functions, is: http://aspn.activestate.com/ASPN/docs/ActivePerl/5.8/lib/Pod/perlretut.html )
(If you are really saying that you want characters all in alphabetic order then that is a quite a challange for RegEx, and would require quite a bit more thought.)

Then you would use the regex in the algorithm that VoteyDisciple gives above:

$timesMatched = preg_match($regex, $_GET['id']);
if ($timesMatched == 0) {
    // It doesn't match; error.
}

Without knowing exactly what you are trying to match I really can't help you more.

As a small aside though I will give you one bit of advice: I dont like referenceing the $_GET array directly, since this code would be very fragile if you ever decided to move the data to a POST delivery method instead.  What I do is use a data_variable class/object that just pulls in all of my data from both POST and GET (And even session variables if you want) and then makes all of that data available to my scripts via that object.  This makes my code much more robust because if I decide to change my HTTP method from POST to GET then none of my code changes at all and everything keeps on running seamlessly.