Solved

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

Posted on 2006-10-30
5
210 Views
Last Modified: 2013-12-12
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'
0
Comment
Question by:huang_ck
5 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 300 total points
ID: 17833505
if(false != strpos($_GET['id'], 'abcd')) {
}

-r-
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17833508
Sorry, should be !==

-r-
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 17833527
$id = @$_GET["id"];

>I want a script so that it can check if the 'id' is in the pattern 'abcd'
do you want to check if id starts with abcd or what?
0
 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 100 total points
ID: 17833616
It really depends on what "abcd" is.  If you just want to make sure that all id strings start, literally, with "abcd" then Roonaan's code is all it takes.  If you're looking to verify the format more precisely then a regular expression is in order.  If, for example, you're looking that id is always like  "order834" for some number, then

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

Expert Comment

by:swymer
ID: 17833852
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question