How do you use PHP to determine page names?

My question while it sounds basic is more complex. I am trying to get php to determine what to do with a function by looking at the pagename. If the page name has a certain word in it I want the function specific to that word to run.
//The URL would look something like this http://www.something.com/index.php?pagname=people_edit
 
//If I had to guess on how to do this I would do something like this.
$pagename = $_REQUEST['pagename'];
 
if($pagename == people_*){ // This is where I am having the problem. I want this to pick up people_add, people_edit, people_delete, etc.
  $do_this = "Something to do";
}
 
if($pagename == animal_*){ // I want this to pick up animal_add, animal_edit, animal_delete, etc.
  $do_this = "Something Different to do";
}

Open in new window

LVL 1
NEComputerAsked:
Who is Participating?
 
cr4ck3rj4ckConnect With a Mentor Commented:
I suggest you read up on regular expressions, they will help you a lot.

I've attached some code below that should help you:
if(preg_match('/.*people_.*/', $_SERVER["REQUEST_URI"])){
  $do_this = "Something to do";
}
 
if(preg_match('/.*animal_.*/', $_SERVER["REQUEST_URI"])){
  $do_this = "Something Different to do";
}

Open in new window

0
 
SreejithGCommented:
You can get the page name from $_SERVER["REQUEST_URI"]
0
 
SreejithGCommented:
sorry, $_SERVER["SCRIPT_NAME"] gives the page name
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
NEComputerAuthor Commented:
actually, I am not trying to get the URI like "index.php" I use the $_SERVER['REQUEST_URI"] already for other purposes. I guess to rephrase my question I am trying to get php to use a wildcard to route variables.  people_* goes to this function and animal_* goes to the other function and so on. People_* couldbe people_add, people_edit, people_delete, people_something.
0
 
SreejithGCommented:
OK, now the question is clear. You need to use regular expressions. See the hel for the function preg_match() in php
0
 
NEComputerAuthor Commented:
Made some minor adjustments to your contribution cr4ck3rj4ack and your solution works great. Thanks to everyone that contributed.
0
 
NEComputerAuthor Commented:
Thank you for your feedback and example they helpded greatly. I actually just changed out your $_SERVER['REQUEST_URI'] and placed in the variable name and everything worked great. Again I appreciate the support.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.