?
Solved

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

Posted on 2006-10-30
5
Medium Priority
?
225 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 1200 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 400 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 400 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

719 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