Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-23-2008, 09:11 AM   PM User | #1
ro1960
New Coder

 
Join Date: Dec 2005
Posts: 80
Thanks: 0
Thanked 0 Times in 0 Posts
ro1960 is an unknown quantity at this point
Doing if statements based on part of a string

I am trying to do a bunch of if and elseif based on part of a variable.

All the variables I want to process in the same manner contain the same string:

submit_xxx
submit_yyy
submit_zzz etc..

So I would like to do something like this:

if ($_GET['var'] OPERATOR "submit_") {....

Does such an operator exists? Or do I have to do use a "==" for each variable I want to process? I guess what I am looking for is an operator that means "contain".

Thanks for any help.
ro1960 is offline   Reply With Quote
Old 05-23-2008, 09:31 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
There are many functions for string manipulation.
See www.php.net/substr
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 05-23-2008, 10:10 PM   PM User | #3
digitalfiz
New Coder

 
Join Date: Mar 2008
Location: Lakeland, FL
Posts: 39
Thanks: 1
Thanked 3 Times in 3 Posts
digitalfiz is an unknown quantity at this point
PHP Code:

if(preg_match('/submit\_/i'$var))
{
//do stuff

That will catch all strings with submit_ in them. Since I'm not sure how your getting $var I didn't include any code. Have no idea if your looping through and array or testing variables hard coded.
digitalfiz is offline   Reply With Quote
Old 05-23-2008, 10:14 PM   PM User | #4
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
I don't know if it makes any difference to use digitalfiz's example or mine, but this is what I use to look for a value within a variable:

PHP Code:
if (strpos($_GET['var'], 'submit_') === FALSE) {
    
//$_GET['var'] does NOT contain submit_
} else {
    
//$_GET['var'] DOES contain submit_

http://www.php.net/strpos
__________________
JDub
http://johnnyzone.com/blog
JohnDubya is offline   Reply With Quote
Old 05-24-2008, 02:31 AM   PM User | #5
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,604
Thanks: 2
Thanked 399 Times in 392 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
strpos() is much faster than preg_match().
Inigoesdr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:25 AM.


Advertisement
Log in to turn off these ads.