phpreplaceexplodestrtr

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


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.


Solution

  • You can use the following code:

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