Search code examples
phpbreadcrumbs

PHP dynamic breadcrumb script creates wrong URL after switching to PHP 8.0


I'm using a PHP script to create a dynamic breadcrumb navigation. I found it several years ago in another forum where it was originally posted in 2006. I updated it by removing some deprecated functions and it worked well so far. It creates the crumbs from the page URL:

$path = $this->_homepath;
$html ='';
$html .= '<a href="https://www.myhomepage.com">Start</a>';

$parts = explode('/', $_SERVER['PHP_SELF']);
$partscount = count($parts);
for($i = 1; $i < ($partscount - 1); $i++) { 
  $path .= $parts[$i] . '/';
  $title = $parts[$i];
  
  if ($this->_usereplacements == true) { 
    reset($this->_replacements);
    foreach($this->_replacements as $search => $replace) {
        $title = str_replace($search, $replace, $title);
    }
  }
  
  if ($this->_replaceunderlines == true) { 
    $title = str_replace('_', ' ', $title);
  }
  
  if ($this->_ucfirst == true) { 
    $title = ucfirst($title);
  }
  
  if ($this->_ucwords == true) { 
    $title = ucwords($title);
  }
  
  $html .= $this->_separator . '<a href="' . $path . '">' . $title . '</a>';
}

The current page title, i.e. the last element of the bradcrumb, is generated as follows:

$title = '';
if ($title == '') { 
  $title = $parts[$partscount-1];
  
  if ($this->_usereplacements == true) { 
   reset($this->_replacements);
   foreach($this->_replacements as $search => $replace) {
        $title = str_replace($search, $replace, $title);
   }
  }
 
  if ($this->_removeextension == true) { 
    $title = substr($title, 0, strrpos($title, '.'));
  }
  
  if ($this->_replaceunderlines == true) { 
    $title = str_replace('_', ' ', $title);
  }
  
  if ($this->_ucfirst == true) { 
    $title = ucfirst($title);
  }
  
  if ($this->_ucwords == true) { 
    $title = ucwords($title);
  }
}
$html .= $this->_separator . '<b>' . $title . '</b>';

I only copied relevant parts of the script and spared eg. the function declarations.

After switching from PHP 7.4 to 8.0 I found that the script is messing up a bit. Consider a page with this url: https://www.myhomepage.com/site1/site2/site3

For the crumbs of site1 the script is now generating the URL https://www.myhomepage.com/site1/site2/site1 insted of https://www.myhomepage.com/site1/, whereas for site2 it shows https://www.myhomepage.com/site1/site2/site1/site2 instead of https://www.myhomepage.com/site1/site2/. As you can see, the whole path https://www.myhomepage.com/site1/site2/ is always added as a prefix before the actual path of the crumb.

I haven't found a solution yet despite looking over this thing for two days. I suppose there have been some changes in PHP 8.0 which causes this behaviour, but I haven't found any clues in the incompatibility list (https://www.php.net/manual/de/migration80.php). I printed §path and $title and they look like they should. When echoing $html after each iteration in the for loop it shows already the wrong URLs. That's why I think that probably the for loop is the problem here. Do you have any suggestions?

Any help on this would be very much appreciated.


Solution

  • I finally was able to figure out what causes the strange behaviour of the script. In the declaration of the breadcrumb constructing function there is $this->_homepath = '/';. Later on I assign $path = $this->_homepath;. When I echo $path its empty instead of showing the aforementioned "/". So I directly set $path = '/'; and now everything works again as it should.