Error my system not clear the -, of the categories
Error my system not clear the -, of the categories
I am implementing a breadcrumb, automatically generated with PHP
<?php
$trail = array(
'path' => 'Cool Stuff',
'to' => 'Animals',
'a' => 'Spiders'
);
$url = 'example.com/products/beauty-woman/fragrance-of-woman/';
$parts = parse_url($url);
$path = pathinfo($parts['path']);
$segments = explode('/', trim($path['dirname'],'/'));
$breadcrumbs = '<a href="/">Home</a>';
$crumb_path = '';
foreach ($segments as $segment)
$crumb_path .= '/' . $segment;
$value = (array_key_exists($segment, $trail)) ? $trail[$segment] : ucfirst($segment);
$breadcrumbs = '<a href="' . $crumb_path . '">' . $value . '</a>';
$breadcrumbs = ucwords(str_replace(['_', '-', '+'], ' ',
$path['filename']));
$breadcrumbs = implode(' » ', $breadcrumbs);
echo $breadcrumbs
?>
But the output is not what I expected, print the following:
Products » Beauty-woman » Fragrance Of Woman
Products » Beauty-woman » Fragrance Of Woman
But how can I show the following result:
Products » Beauty Woman » Fragrance of woman
Products » Beauty Woman » Fragrance of woman
$value
change
. $value .
to . ucwords(str_replace(['_', '-', '+'], ' ', $value)) .
– Jeff
15 hours ago
. $value .
. ucwords(str_replace(['_', '-', '+'], ' ', $value)) .
@Jeff It's perfect, you can put this like that at the end
Fragrance of woman
– Fernando
15 hours ago
Fragrance of woman
1 Answer
1
You don't str_replace()
the $value
when you actually assign it to the array $breadgrumbs
.
str_replace()
$value
$breadgrumbs
So change that line
$breadcrumbs = '<a href="' . $crumb_path . '">' . $value . '</a>';
to
$breadcrumbs = '<a href="' . $crumb_path . '">' . ucwords(str_replace(['_', '-', '+'], ' ', $value)) . '</a>';
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
the str_replace should be happening when you acutally assign the strings to the array. So for
$value
– Jeff
15 hours ago