1

My original string is

one,two,three,four,five,

I need separate each word as a link

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

My code is

$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>';
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Steve Bals
  • 1,949
  • 6
  • 22
  • 32

4 Answers4

2

This would suffice..

<?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>
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
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 
Benjamin
  • 2,257
  • 1
  • 15
  • 24
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;
hindmost
  • 7,125
  • 3
  • 27
  • 39
-1

Try this code:

$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>';
    }
}
BaBL86
  • 2,602
  • 1
  • 14
  • 13