How to explode URL and set "a" tag to every word(s) between "/"?

103 Views Asked by At

For example, if URL is http://localhost/category/news/old-stuff then this function gives me this result:

<a>newsold stuff</a>

Question:

how to put every word(s) between / to <a> tag ?

Example:

<a>news</a> <a>old stuff</a>

Function i am using:

$address =  $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$current = strtr($address, array('localhost' => '', 'category' => '', '/' => '', '-' => ' ' ));
echo '<a href="#">'. $current .'</a>';

Thanks for any answers and sorry for bad english.

2

There are 2 best solutions below

0
On BEST ANSWER

You can use the following code:

$ex = explode("/",$_SERVER["REQUEST_URI"]);
foreach($ex as $val){
    echo '<a>'.str_replace('-',' ',$val).'</a>';
}
0
On

Try this :

$url =  $_SERVER['REQUEST_URI'];
$tags = explode('/', $url);
foreach ($tags as $tag) {
    echo "<a href='#'>" . str_replace('-',' ',$tag) . "</a> ";
}