1
<?php

    $test = ' /clothing/men/tees';

    $req_url = explode('/', $test);

    $c = count($req_url);

    $ex_url = 'http://www.test.com/';

    for($i=1; $c > $i; $i++){

        echo '/'.'<a href="'.$ex_url.'/'.$req_url[$i].'">

                <span>'.ucfirst($req_url[$i]).'</span>

            </a>';
        //echo '<br/>'.$ex_url;....//last line
    }
?>

OUTPUT - 1 //when comment last line

/ Clothing / Men / Tees

OUTPUT - 2 //when un-comment last line $ex_url shows

/ Clothing 
http://www.test.com// Men 
http://www.test.com// Tees 
http://www.test.com/

1. Required output -

In span - / Clothing / Men / Tees and last element should not be clickable

and link should created in this way

http://www.test.com/clothing/Men/tees -- when click on Tees

http://www.test.com/clothing/Men --  when click on Men

...respectively

2. OUTPUT 2 why it comes like that

4
  • Output 1 does not work? Commented Dec 2, 2014 at 8:22
  • 1
    An array starts at 0 not at 1 Commented Dec 2, 2014 at 8:22
  • @Naruto The string starts with a slash as well... Commented Dec 2, 2014 at 8:24
  • @Naruto i know that. but in my case $req_url[0] is blank. Commented Dec 2, 2014 at 9:02

4 Answers 4

2

Try this:

   <?php
    $test = '/clothing/men/tees';
    $url = 'http://www.test.com';
    foreach(preg_split('!/!', $test, -1,  PREG_SPLIT_NO_EMPTY) as $e) {
    $url .= '/'.$e;
        echo '/<a href="'.$url.'"><span>'.ucfirst($e).'</span></a>';
    }
    ?>

Output:

/Clothing/Men/Tees

HTML output:

/<a href="http://www.test.com/clothing"><span>Clothing</span></a>/<a href="http://www.test.com/clothing/men"><span>Men</span></a>/<a href="http://www.test.com/clothing/men/tees"><span>Tees</span></a>
Sign up to request clarification or add additional context in comments.

8 Comments

thanks..can u explain your code? whats wrong when i comment last line?
@Prashant I'm not an expert but I know something about HTML DOM structure, whenever you want to display elements inline, close to each other, you should render them inline only. Like <a>1</a><a>2</a>. You cann't use <br> tag, this is use for breaking line
preg_split('!/!', $test, -1, PREG_SPLIT_NO_EMPTY) explain this
@Prashant preg_split — Split string by a regular expression, '!/!' pattern to search for, you can also use '/\//', $test - your string, -1 - Limit (-1, 0, NULL No Limit), PREG_SPLIT_NO_EMPTY - return only non-empty For more information you should visit real source docs.php.net/preg_split
i have ' /clothing/men/tees/' this. output i got home /clothing/men . i dont want space before clothing
|
1

Try using foreach() to iterate the array and you'll have to keep track of the path after the url. Try it like so (tested and working code):

<?php

    $test = '/clothing/men/tees';
    $ex_url = 'http://www.test.com';
    $items = explode('/', $test);
    array_shift($items);

    $path = '';
    foreach($items as $item) {
        $path .= '/' . $item;
        echo '/ <a href="' . $ex_url . $path . '"><span>' . ucfirst($item) . '</span></a>';
    }

Comments

1

Try this.

<?php

$test = '/clothing/men/tees';
$req_url = explode('/', ltrim($test, '/'));
$ex_url = 'http://www.test.com/';

$stack = array();
$reuslt = array_map(function($part) use($ex_url, &$stack) {
    $stack[] = $part;
    return sprintf('<a href="%s%s">%s</a>', $ex_url, implode('/', $stack), ucfirst($part));
}, $req_url);


print_r($reuslt);

1 Comment

your code show error Message : syntax error, unexpected '['
-1
<?php
    $sTest= '/clothing/men/tees';
    $aUri= explode( '/', $sTest );
    $sBase= 'http://www.test.com';  // No trailing slash

    $sPath= $sBase;  // Will grow per loop iteration
    foreach( $aUri as $sDir ) {
        $sPath.= '/'. $sDir;
        echo ' / <a href="'. $sPath.'">'. ucfirst( $sDir ). '</a>';  // Unnecessary <span>
    }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.