Solved

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

Posted on 2006-10-30
5
213 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

713 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