PHP简单修改/修正

发布于 2024-08-28 06:51:57 字数 437 浏览 2 评论 0原文

我从某人那里得到了这段代码,创建动态面包屑几乎是完美的,但是有一点小故障,因为它在面包屑之前回显了两个分隔线:

$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
    echo ucfirst(str_replace(array(".php","_"),array(""," "),'>' . $crumb));
}

它回显:

“>>内容>通用>文件”

我想要它做什么看起来像是

“content>common>1”,

如果有人能告诉我如何为数组中除最后一个(文件)之外的所有项目添加链接,我将不胜感激?

非常感谢大家,这个网站通过示例对我学习 php 帮助很大!

I got this code from someone, it's almost perfect to create a dynamic breadcrumb, but there just a little glitch because it echoes two dividers before the breadcrumb:

$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
    echo ucfirst(str_replace(array(".php","_"),array(""," "),'>' . $crumb));
}

it echoes:

">>content>common>file"

what I want it to look like is

"content>common>1"

and also I will deeply appreciate if someone can tell me how can I add links for all the items in the array except the last one (file)?

Thank you so much everybody, this website really helped me a lot to learn php by examples!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2024-09-04 06:51:57

也许这样的事情会做:


//去掉空的部分
$crumbs = array_filter($crumbs);

$result = array();
$path = '';
foreach($crumbs as $crumb){
    $path .= '/' . $crumb;
    $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
    $result[] = "<a href=\"$path\">$name</a>";

}

echo implode(' > ', $result);


已更新

$result = array();
$path = '';
$num = count($crumbs);
for ($j=0; $j<$num; $j++) {
    $crumb = $crumbs[$j];
    if ($crumb == '') {
        continue;   
    }
    $path .= '/' . $crumb;
    $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
    if ($j < ($num - 1)) {
        $result[] = "<a href=\"$path\">$name</a>";
    } else {
        $result[] = $name;
    }
}

echo implode(' > ', $result);

Maybe something like this will do:


//get rid of empty parts
$crumbs = array_filter($crumbs);

$result = array();
$path = '';
foreach($crumbs as $crumb){
    $path .= '/' . $crumb;
    $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
    $result[] = "<a href=\"$path\">$name</a>";

}

echo implode(' > ', $result);


Updated

$result = array();
$path = '';
$num = count($crumbs);
for ($j=0; $j<$num; $j++) {
    $crumb = $crumbs[$j];
    if ($crumb == '') {
        continue;   
    }
    $path .= '/' . $crumb;
    $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
    if ($j < ($num - 1)) {
        $result[] = "<a href=\"$path\">$name</a>";
    } else {
        $result[] = $name;
    }
}

echo implode(' > ', $result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文