2014-12-02 73 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