• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

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

0
NEComputer
Asked:
NEComputer
  • 3
  • 3
1 Solution
 
SreejithGCommented:
You can get the page name from $_SERVER["REQUEST_URI"]
0
 
SreejithGCommented:
sorry, $_SERVER["SCRIPT_NAME"] gives the page name
0
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SreejithGCommented:
OK, now the question is clear. You need to use regular expressions. See the hel for the function preg_match() in php
0
 
cr4ck3rj4ckCommented:
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now