使用php获取页面目录

时间:2017-02-13 18:23:59

标签: php

我想要做的是获取我的页面目录

Home / Clothes / Something

我试过这个,但我对此并不了解很多,但它不能正常工作

<?php

// current directory
echo getcwd() . "\n";

chdir('cvs');

// current directory
echo getcwd() . "\n";

?>

这是我需要做的例子

我的页面是example.com/clothes/something.php

在那个页面上“something.php”我想回应一下这样的事情

<a href="example.com">Home</a> / <a href="example.com/clothes/">clothes</a> / <a href="example.com/clothes/something.php">Something</a>

我忘记了这个叫什么,但希望你理解

<?php 
    $path = $_SERVER["PHP_SELF"];
    $parts = explode('/',$path);
    if (count($parts) < 2)
    {
    echo("home");
    }
    else
    {
    echo ("<a href=\"/\">Home</a> &raquo; ");
    for ($i = 1; $i < count($parts); $i++)
        {
        if (!strstr($parts[$i],"."))
            {
            echo("<a href=\"");
            for ($j = 0; $j <= $i; $j++) {echo $parts[$j]."/";};
            echo("\">". str_replace('-', ' ', $parts[$i])."</a> » ");
            }
        else
            {
            $str = $parts[$i];
            $pos = strrpos($str,".");
            $parts[$i] = substr($str, 0, $pos);
            echo str_replace('-', ' ', $parts[$i]);
            };
        };
    };  
?>

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

library(magrittr)
Websites<-"UK, DE, CA, US" %>% gsub("(\\w+)", "'\\1'", .)
print(Websites)
#[1] "'UK', 'DE', 'CA', 'US'"

您可能需要稍微调整一下,但这是基本想法。

[编辑]

也许是这样的:

$subPath = '';
$path = explode('/', $_SERVER['REQUEST_URI']);
array_shift($path);

foreach ($path as $segment) {
  $subPath.= $segment.'/';
  echo "<A HREF=\"http://www.php.loc/$subPath\">$segment</A> / ";      
}

如果你的PHP版本在5.4.0之前,那么你需要一个不同的数组语法:

$Pages = [
  '/clothes' => 'Clothes',
  '/clothes/something.php' => 'Something'
];

$subPath = '';
$BreadCrumbs = [];
$path = explode('/', $_SERVER['REQUEST_URI']);
array_shift($path);

foreach ($path as $segment) {
  $subPath.= '/'.$segment;
  $BreadCrumbs[] = "<A HREF=\"http://www.php.loc$subPath\">$Pages[$subPath]</A>";
}

echo implode(' &gt; ', $BreadCrumbs);

[编辑]

好的,最后一次:

$Pages = array(
  '/clothes' => 'Clothes',
  '/clothes/something.php' => 'Something'
);

$BreadCrumbs = array();