2014-12-02 43 views
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 
    } 
?> 

輸出 - 1 //當評論最後一行如何在PHP中連續連接字符串?

/ Clothing/Men/Tees 

輸出 - 2 //當取消註釋最後一行$ ex_url顯示

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

1.所需的輸出 -

跨度 - /服裝/男裝/ T恤和最後一個元素不應該點擊

和鏈路應該以這種方式

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

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

創建...分別

2.輸出2爲什麼談到這樣

+0

輸出1不起作用? – 2014-12-02 08:22:04

+1

從0開始的數組不是1 1 – Naruto 2014-12-02 08:22:16

+0

@Naruto字符串以斜槓開始以及... – RichardBernards 2014-12-02 08:24:50

回答

2

試試這個:

<?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>'; 
    } 
    ?> 

輸出:

/Clothing/Men/Tees 

HTML輸出:

/<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> 
+0

謝謝..你可以解釋你的代碼?當我評論最後一行時有什麼不對? – 2014-12-02 09:06:25

+0

@Prashant我不是專家,但是我知道關於HTML DOM結構的一些東西,無論何時想要顯示內聯元素,彼此靠近,都應該只將它們渲染爲內聯。像 。你不能使用
標記,這是用於突破線 – 2014-12-02 09:25:07

+0

'preg_split('!/!',$ test,-1,PREG_SPLIT_NO_EMPTY)'解釋這個 – 2014-12-02 09:39:28

-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> 
    } 
?> 
1

嘗試使用foreach()迭代該數組,你將不得不跟蹤url後的路徑。嘗試像這樣(測試和工作代碼):

<?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>'; 
    } 
1

試試這個。

<?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); 
+0

你的代碼顯示錯誤'消息:語法錯誤,意外'['' – 2014-12-02 09:41:43