Solved

How do I create variables for each directory in a string?

Posted on 2014-04-09
2
133 Views
Last Modified: 2014-04-09
I am trying to create variables for directories to be used later within the site as titles.

<?php echo $title_1 ?>

<?php echo $title_2 ?>

from          /dir1/dir2/dir3

So far I am able to pick up the first title in the directory but having problems extracting the second or third titles...  Below is where I am...

Any thoughts?

<?
    if($location = substr(dirname($_SERVER['PHP_SELF']), 1))
        $dirlist = explode('/', $location);
    else
        $dirlist = array();
    
    $count = array_push($dirlist, basename($_SERVER['PHP_SELF']));
    
    $address = 'http://'.$_SERVER['HTTP_HOST'];
    
    for($i = 0; $i < $count; $i++)
    
         ($activepath .= str_replace("index.php","",'/'.  $dirlist[$i]));
    
    $value = $activepath;
    $topdir = strtok($value, " / ");
    $title_1 = ucwords($topdir);
    
    
    $value2 = $activepath;
    $secdir .= str_replace("/", "", " ", strtok($value2, " / "))  ;
    $title_2 = ucwords($secdir);

Open in new window

0
Comment
Question by:morako
2 Comments
 
LVL 17

Accepted Solution

by:
selvol earned 500 total points
ID: 39988459
<?php
/* get array containing each directory name in the path */
$parts = explode("/", dirname($_SERVER['REQUEST_URI']));

foreach ($parts as $key => $dir) {
switch ($dir) {

}
$url = "";
for ($i = 1; $i <= $key; $i++)
{ $url .= $parts[$i] . "/"; }

}
echo $parts[1];
echo $parts[2];
echo $parts[3];
?>

Open in new window



Selvol
0
 

Author Closing Comment

by:morako
ID: 39989095
Thanks..  ;-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now