Solved

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

Posted on 2006-10-30
5
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

729 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