Solved

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

Posted on 2014-04-09
2
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 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