iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Programming Programmers can seek advice or just show off their creations inside this forum.

Share Thread: & Tags

Share Thread:

Tags
code, php, request

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-06-2009, 06:56 AM
WebProWorld Pro
 
Join Date: Oct 2005
Location: Belgium
Posts: 215
Gert Leroy RepRank 0
Default PHP string function - easy question

hi, i have a question on how to check strings in PHP.
Situation:
when i'm at the url www.mydomain.com/fitness i want to show the image "personal_trainer.jpg"
see code:

<?php
$img = ""; // variable for the name of the image
switch ($_SERVER['REQUEST_URI']) // requesting the url of the page i'm at
{
case "/fitness/":
$img = "personal_trainer.jpg";
break;
...
}
?>

That works fine. Now, i also want to show that same image for variations of this url, like
/fitness/?biz=24 or /fitness/?biz=1542

can anyone adjust the above code to make this happen?
thanks.
Reply With Quote
  #2 (permalink)  
Old 11-06-2009, 07:56 AM
kgun's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 5,723
kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10
Default Re: PHP string function - easy question

Quote:
Originally Posted by Gert Leroy View Post
hi, i have a question on how to check strings in PHP.
My bolding
  1. Using Regular Expressions in PHP [PHP & MySQL Tutorials]
  2. Look at the different string functions on the php.net site. Example: PHP: Strings - Manual
  3. If you use external libraries like pear, there should be advanced string functions.
General search: regular expressions php
Quote:
Originally Posted by Gert Leroy View Post
That works fine. Now, i also want to show that same image for variations of this url, like
/fitness/?biz=24 or /fitness/?biz=1542

can anyone adjust the above code to make this happen?
thanks.
Isn't that simple conditional programming?

Last edited by kgun; 11-06-2009 at 08:00 AM.
Reply With Quote
  #3 (permalink)  
Old 11-06-2009, 08:36 AM
WebProWorld Pro
 
Join Date: Oct 2005
Location: Belgium
Posts: 215
Gert Leroy RepRank 0
Default Re: PHP string function - easy question

Conditional programming. yes.
Simple. haha.not for me.

i suppose i should use the PHP function "stripos" to get the job done.

if anyone could just feed the solution right into my mouth..
Reply With Quote
  #4 (permalink)  
Old 11-06-2009, 01:08 PM
wige's Avatar
Moderator
WebProWorld Moderator
 
Join Date: Jun 2006
Location: United States
Posts: 2,661
wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9
Default Re: PHP string function - easy question

I would generally stay away from case statements. While they have a purpose, anything that has to be explicitly stopped to prevent catastrophic failure is a bad idea. For this, I would use ereg() to check for the existence of your text. Since you are testing based on the beginning of the string you would use the regular expression "^/fitness/", so the code would be ereg('^/fitness', $_SERVER['REQUEST_URI']) or in the full code:
PHP Code:
if (ereg('^/fitness/'$_SERVER['REQUEST_URI'])) {
   
blah blah

__________________
The best way to learn anything, is to question everything.
Reply With Quote
  #5 (permalink)  
Old 11-06-2009, 05:43 PM
WebProWorld Veteran
 
Join Date: Apr 2004
Posts: 349
imvain2 RepRank 1
Default Re: PHP string function - easy question

Although, Wige is correct that ereg would be a great method for doing this, if you aren't familiar with regular expressions, they tend to get tedious.

Keeping with Wige's if statement, why not just use something like strpos? It is easy to understand and easy to use and make updates to.

Like Wige says case statements have their purpose and are highly versatile, I believe regular expressions over complicate otherwise simple solutions.

PHP Code:
if(strpos($haystack,$needle) !== false ){


in the above example, $haystack would be $_SERVER['REQUEST_URI'] and needle would be "/fitness/".

Please note the !== , the double equal sign is proper format for this specific call.
PHP: strpos - Manual

Quote:
Originally Posted by wige View Post
I would generally stay away from case statements. While they have a purpose, anything that has to be explicitly stopped to prevent catastrophic failure is a bad idea. For this, I would use ereg() to check for the existence of your text. Since you are testing based on the beginning of the string you would use the regular expression "^/fitness/", so the code would be ereg('^/fitness', $_SERVER['REQUEST_URI']) or in the full code:
PHP Code:
if (ereg('^/fitness/'$_SERVER['REQUEST_URI'])) {
   
blah blah

Reply With Quote
  #6 (permalink)  
Old 11-06-2009, 06:38 PM
Uncle Dog's Avatar
WebProWorld Pro
 
Join Date: Apr 2008
Location: Scotland
Posts: 269
Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5
Default Re: PHP string function - easy question

Quote:
Originally Posted by imvain2 View Post
...if you aren't familiar with regular expressions, they tend to get tedious.
...and if you don't use them you won't get familiar with them, which IS tedious. I'm with wige on this one. I like regular expressions.
__________________
There are 10 types of people in this world: those who understand binary and those who don't.
Reply With Quote
Reply

  WebProWorld > Computer Assistance > Programming

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
An Easy CSS DIV question spenland Graphics & Design Discussion Forum 3 05-24-2006 04:15 PM
B2B Or Not B2B, That’s An Easy Question dutter Insider Reports 1 08-18-2005 08:02 PM
Just read the SEO is BS string,.. mogrady Search Engine Optimization Forum 3 11-23-2004 05:31 PM
Quick, easy question... I think. mlevenhagen Search Engine Optimization Forum 4 10-31-2004 06:06 PM
Easy question for the experts tenaki Graphics & Design Discussion Forum 7 11-28-2003 03:43 PM


All times are GMT -4. The time now is 12:59 PM.



Search Engine Optimization by vBSEO 3.3.0