在<a> by php

时间:2016-01-06 10:35:33

标签: php

i have some values in comma separated like : $tag=jan,feb,mar,apr,may

Now i want to do this

<a href="xyz.php?tag=jan">jan</a> 
<a href="xyz.php?tag=feb">feb</a> 
<a href="xyz.php?tag=mar">mar</a> 
<a href="xyz.php?tag=apr">apr</a> 
<a href="xyz.php?tag=may">may</a> 

help me out of this. thank you in advance

6 个答案:

答案 0 :(得分:3)

<?php
$tag="jan,feb,mar,apr,may";
$tags = explode(',', $tag); //Explode tags into an array
foreach($tags as $vals)//Loop through tags
{
    ?>
    <a href="xyz.php?tag=<?php echo $vals; ?>"><?php echo $vals; ?></a>
    <?php //Echo onto page
}
?>

答案 1 :(得分:0)

就这样做:

<?php
$tag='jan,feb,mar,apr,may';
foreach(explode(',', $tag) as $val){
echo '<a href="xyz.php?tag='.$val.'">'.$val.'</a><br>'; 
}
?>

答案 2 :(得分:0)

您可以使用explode将它们放入数组中。

$tag = "jan,feb,mar,apr,may,jun,jul"; //etc

$months = explode(",", $tag);

爆炸功能会将$months设置为如下所示:

$months = [
    'jan',
    'feb',
    'mar',
    'apr',
    'may',
    'jun',
    'jul'
];

然后,您可以使用foreach循环播放您已创建的新阵列。

foreach($months as $month) {
    echo "<a href='xyz.php?tag=$month'>$month</a><br>";
}

答案 3 :(得分:0)

您可以简单地用逗号分隔字符串以获得数月的数组。

现在,遍历数组并打印几个月的链接。

<?php
$tag="jan,feb,mar,apr,may";
$tags = explode(',', $tag);
if (! empty($tags)) {
  foreach ($tags as $tag) {
  ?>
  <a href="xyz.php?tag=<?php echo $tag;?>"><?php echo $tag;?></a>   
  <?php
  }
}
?>

答案 4 :(得分:0)

foreach (explode(",", $tag) as $month) { //split by comma
echo '<a href="xyz.php?tag=' . $month . "</a>\n"; //just echo it
}

答案 5 :(得分:0)

一种“功能性”方法,利用explode()array_map()implode()

$tags="jan,feb,mar,apr,may";
echo implode("\n",array_map(function($tag) {
    return "<a href=\"xyz.php?tag=$tag\">$tag</a>";
}, explode(',',$tags)))

或者,在array_reduce()的帮助下,无需额外拨打implode

$tags="jan,feb,mar,apr,may";
echo array_reduce(explode(',',$tags), function($acc, $tag) {
    return $acc."<a href=\"xyz.php?tag=$tag\">$tag</a>\n";
}, '')

当然,如果你已经有数组格式的标签,那么你可以跳过explode