PHP中没有逗号的单独字符串

时间:2014-02-15 10:29:38

标签: php arrays string function

我的原始字符串是

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

4 个答案:

答案 0 :(得分: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 :(得分: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 

答案 2 :(得分: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;

答案 3 :(得分:-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>';
    }
}