2014-02-15 49 views
1
逗號分隔字符串

我原來的字符串是沒有PHP

one,two,three,four,five, 

我需要單獨的每個字爲紐帶

<a href="">one</a>, <a href="">two</a>, <a href="">three</a>, ... 

我的代碼是

$plat = $row['reg']; 
foreach ($plat as $key => $pv) { 
    $pl[] = implode(',', $pv); 
} 
for ($p = 1; $p = sizeof($pl); $i++) { 
    echo '<a href="#" rel="tag">' . $pl[i] . '</a>'; 
} 
+0

簡單使用'爆炸( 「」,$ STR)'與陣列逗號分割字符串並根據你的需要使用它 –

+1

@SatishSharma不僅他需要把第二個interator放在第一個像在我的代碼。 – BaBL86

回答

2

這就夠了..

<?php 
$str='one,two,three,four,five'; 
$arr=explode(',',$str); 
foreach($arr as $val) 
{ 
echo "<a href=''>$val</a>, "; 
} 

OUTPUT :

<a href=''>one</a>, <a href=''>two</a>, <a href=''>three</a>, <a href=''>four</a>, <a href=''>five</a> 
-1

嘗試此代碼:

$plat=$row['reg']; 
foreach ($plat as $key=> $pv) { 
    $pl[] = explode(',', $pv); 
    for($p=1;$p=sizeof($pl); $i++) { 
     echo '<a href="#" rel="tag">'.trim($pl[i]).'</a>'; 
    } 
} 
0
$string = 'one,two,three,four,five'; 
$tag_open = '<a href="#" rel="tag">'; 
$tag_close = '</a>'; 
echo $tag_open. implode($tag_close.', '.$tag_open, explode(',', $string)). $tag_close; 
1
<?php 
    $str='one two three four five'; 
     $arr=explode(' ',$str); 
     foreach($arr as $val) 
     { 
     echo "<a href=''>$val</a>, "; 
     } 
     this Code to separate blank spacesepration