爆炸功能codeigniter不起作用

时间:2015-07-18 17:49:15

标签: php database codeigniter

我只是尝试在标签系统中使用爆炸功能,但它不能正常工作,上半部分是工作但下半部分不起作用,我会解释我的代码和问题

Database structure : In a database structure  create a one col for tag storage
Tag Col : first_tag,second_tag,tag,third_tag

现在我的代码是:

<?php
if(!empty($data)) {                                 
$str1=(explode(",",$data->tags));
$total=count($str1);}   
if($data) { 
for($i=0;$i<$total;$i++)
{ ?>
<a href="#"><?php echo explode('_',$str1[$i]);?></a>
<?php   }} ?>

结果是:First Explode功能正常工作

  <a href="#">first_tag</a>
  <a href="#">second_tag</a>
  <a href="#">tag</a>
  <a href="#">third_tag</a>

但是第二次爆炸功能不起作用:我需要这种结构

  <a href="#">first tag</a>
  <a href="#">second tag</a>
  <a href="#">tag</a>
  <a href="#">third tag</a>

我需要这个结构,请检查我的代码

2 个答案:

答案 0 :(得分:2)

也许是这样的事情

if(!empty($data)) {                                 
  $str1=(explode(",",$s));
  foreach($str1 as $str)
        { ?>
<a href="#"><?php echo str_replace('_', ' ', $str);?></a>
<?php   }
} ?>

答案 1 :(得分:0)

使用preg_match而不是像这样

<?php
$data= "first_tag,second_tag,tag,third_tag";

if(!empty($data)) {                                 
$str1=(explode(",",$data));
$total=count($str1);}   
if($data) { 
for($i=0;$i<$total;$i++)
{ 
preg_match( "/([a-z]+)_([a-z]+)|([a-z]+)/",$str1[$i],$matches);
?>
<a href="#"><?php echo $matches[1]." ".$matches[2]." ".$matches[3] ;?></a>
<?php   }} ?>

这将产生

<a href="#">first tag </a>
<a href="#">second tag </a>
<a href="#">  tag</a>
<a href="#">third tag </a>
相关问题