Log In · Register

 
 
Closed TopicStart new topic
PHP variables and stuffs
Mikeplyts
post Feb 15 2010, 09:51 PM
Post #1


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



I'm working on a little something-something and I was wondering if I could use variables or placeholders or whatever in a switch function based on the URL. Sort of like Apache.

Um, something like this:
CODE
<?php
$uri = $_SERVER['SCRIPT_NAME'];

switch($uri) {
    case '/$1/$2/$3/index.php':
        echo 'Under 3 directories.';
        break;
    case '/$1/$2/$3/$4/index.php':
        echo 'Under 4 directories.';
        break;
}
?>


Get what I mean? And yes, I've tried the above but it doesn't seem to work. tongue.gif Help would be appreciated.
 
mipadi
post Feb 15 2010, 10:40 PM
Post #2


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



No, but something like the following could work:

CODE
<?php

$uri = $_SERVER['SCRIPT_NAME'];
$dirs = split("/", $uri)     // Assuming split("/", "/$1/$2/$3/index.php")
                             // returns ["", "$1", "$2", "$3", "index.php"]
$dirs = count($dirs) - 2;    // Get rid of extra 2 entries (empty string at
                             // beginning and "index.php" at end)
switch ($dirs) {
case 3:
    echo "Under 3 directories";
    break;
case 4:
    echo "Under 4 directories";
    break;
}
>?
 
fixtatik
post Feb 16 2010, 01:20 AM
Post #3


Senior Member
******

Group: Member
Posts: 1,237
Joined: May 2008
Member No: 648,123



You could also do it like this:

CODE
echo 'Under ' . (substr_count($_SERVER['SCRIPT_NAME'], '/') - 1) . ' directories';

substr_count() counts the occurrences of a string within a string.

Side note, split() works, but it was deprecated in PHP5.3, and they got rid of it in PHP6. preg_split() is the new one, or for simpler purposes, explode().
 
Mikeplyts
post Feb 16 2010, 08:37 PM
Post #4


Mel Blanc was allergic to carrots.
*******

Group: Official Designer
Posts: 6,371
Joined: Aug 2008
Member No: 676,291



Works great. Thanks guys.

Topic Closed. mellow.gif
 
manny-the-dino
post Apr 2 2010, 01:00 PM
Post #5


Senior Member
*******

Group: Administrator
Posts: 8,629
Joined: Jan 2007
Member No: 498,468



Topic Closed, and Moved to Resolved Topics. Please PM a moderator if you would like this reopened.
 

Closed TopicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: