Solved

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

Posted on 2014-04-09
2
140 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 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