I have following PHP script for displaying breadcrumbs… · The Chat Archive
To ensure your breadcrumbs display only the path components and exclude any query parameters (like ?tab=changePassword), you can modify your PHP script to parse the URL and extract only the path part. Here's how you can achieve this:
Use parse_url to Extract the Path:
The parse_url function allows you to dissect the URL and retrieve specific components. By extracting only the path, you can avoid including query parameters in your breadcrumb.
Adjust the Breadcrumb Generation Logic:
After obtaining the clean path, you can proceed to generate the breadcrumb as you did before, ensuring that only the necessary segments are included.
Updated PHP Script
php
<?php
// Get the current URL
$currentUrl = $_SERVER['REQUEST_URI'];
// Use parse_url to extract the path component only, excluding query parameters
$parsedUrl = parse_url($currentUrl);
$path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '/';
// Split the path into its components
$pathComponents = explode('/', trim($path, '/'));
// Base URL for the links
$baseUrl = "https://trackback.email/";
// Start the breadcrumb HTML
echo '<ol class="breadcrumb m-0">';
// Initialize the current path for building URLs
$currentPath = $baseUrl;
foreach ($pathComponents as $index => $component) {
// Append the current component to the path
$currentPath .= $component . '/';
// Extract the filename without extension (handles cases like "trackback.email")
$componentName = pathinfo($component, PATHINFO_FILENAME);
// Replace any non-word characters with a space (e.g., "my-account" becomes "my account")
$componentName = preg_replace('/[^\w]/', ' ', $componentName);
// Trim any extra spaces
$componentName = trim($componentName);
// Capitalize each word (e.g., "my account" becomes "My Account")
$componentName = ucwords($componentName);
// Check if it's the last component to determine if it should be a link or plain text
if ($index === count($pathComponents) - 1) {
// Last component: display as plain text without a link
echo '<li class="breadcrumb-item active" aria-current="page">' . htmlspecialchars($componentName) . '</li>';
} else {
// Intermediate components: display as links
echo '<li class="breadcrumb-item"><a href="' . htmlspecialchars($currentPath) . '">' . htmlspecialchars($componentName) . '</a></li>';
}
}
echo '</ol>';
?>
This ensures that any query strings (like ?tab=changePassword) are excluded from the breadcrumb generation.
Sanitizing Output:
php
htmlspecialchars($componentName)
Using htmlspecialchars ensures that any special characters in the path components are safely outputted as HTML, preventing potential XSS attacks.
Handling the Base URL:
Ensure that the $baseUrl variable correctly reflects the base of your site. In your example, it was set to "https://trackback.email/", but your actual site seems to be "https://valkya.lan/trackback.email/". Adjust as necessary.
Optional Enhancements:
Active Class for the Last Breadcrumb: The active class and aria-current="page" attribute are added to the last breadcrumb item for better accessibility and styling.
Consistent Capitalization: The script ensures that each word in the breadcrumb is capitalized for a cleaner appearance.