Solved

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

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

Technology Partners: 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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 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