如何从面包屑中删除最后一个 » 并在 php 中激活最后一个文本?
How remove last » from breadcrumbs and active last text in php?
下面是我的面包屑格式,这里我需要删除面包屑的最后一个»符号和活动的最后一个文本,下面是我的php代码和数组。
主页 » 手机和配件 » 配件 »
<div class="txt">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a> »
<?php } ?>
</div>
Array
(
[0] => Array
(
[text] => Home
[href] => http://localhost/opencart/index.php?route=common/home
)
[1] => Array
(
[text] => Mobiles & Accessories
[href] => http://localhost/opencart/index.php?route=product/category&path=33
)
[2] => Array
(
[text] => Accessories
[href] => http://localhost/opencart/index.php?route=product/category&path=33_62
)
)
你的数组数据:
<?php
$breadcrumbs =
[
[
'text' => 'Home',
'href' => 'http://localhost/opencart/index.php?route=common/home'
],
[
'text' => 'Mobiles & Accessories',
'href' => 'http://localhost/opencart/index.php?route=product/category&path=33'
],
[
'text' => 'Accessories',
'href' => 'http://localhost/opencart/index.php?route=product/category&path=33_62'
],
];
?>
解决方案:
<?php
$breadcrumbsCount = count($breadcrumbs);
for ($i = 0; $i < $breadcrumbsCount; $i++) {
if ($i != $breadcrumbsCount - 1) {
echo '<a href="'.$breadcrumbs[$i]['href'].'">'.$breadcrumbs[$i]['text'].'</a> »' . PHP_EOL;
} else {
echo '<a href="'.$breadcrumbs[$i]['href'].'" class="active">'.$breadcrumbs[$i]['text'].'</a>' . PHP_EOL;
}
}
?>
此示例已在我的本地计算机上进行了测试。有效。
希望此解决方案对您有所帮助。
此致。
下面是我的面包屑格式,这里我需要删除面包屑的最后一个»符号和活动的最后一个文本,下面是我的php代码和数组。
主页 » 手机和配件 » 配件 »
<div class="txt">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a> »
<?php } ?>
</div>
Array
(
[0] => Array
(
[text] => Home
[href] => http://localhost/opencart/index.php?route=common/home
)
[1] => Array
(
[text] => Mobiles & Accessories
[href] => http://localhost/opencart/index.php?route=product/category&path=33
)
[2] => Array
(
[text] => Accessories
[href] => http://localhost/opencart/index.php?route=product/category&path=33_62
)
)
你的数组数据:
<?php
$breadcrumbs =
[
[
'text' => 'Home',
'href' => 'http://localhost/opencart/index.php?route=common/home'
],
[
'text' => 'Mobiles & Accessories',
'href' => 'http://localhost/opencart/index.php?route=product/category&path=33'
],
[
'text' => 'Accessories',
'href' => 'http://localhost/opencart/index.php?route=product/category&path=33_62'
],
];
?>
解决方案:
<?php
$breadcrumbsCount = count($breadcrumbs);
for ($i = 0; $i < $breadcrumbsCount; $i++) {
if ($i != $breadcrumbsCount - 1) {
echo '<a href="'.$breadcrumbs[$i]['href'].'">'.$breadcrumbs[$i]['text'].'</a> »' . PHP_EOL;
} else {
echo '<a href="'.$breadcrumbs[$i]['href'].'" class="active">'.$breadcrumbs[$i]['text'].'</a>' . PHP_EOL;
}
}
?>
此示例已在我的本地计算机上进行了测试。有效。
希望此解决方案对您有所帮助。
此致。