PHP:使用<a href=""> anchor tags

时间:2015-07-02 21:03:28

标签: php

I am using this code to display a comma seperated list but removing the last comma.

how can i add a link (a href) to each item displayed?

$tags = explode(',', $ret["tags"]);
echo join(' , ', array_filter(
        array_merge(
            array(
                join(', ', array_slice($tags, 0, -1))
            ), array_slice($tags, -1)
        )
    )
);

1 个答案:

答案 0 :(得分:3)

这就是我重做代码的方式:

$tags = "link1,link2,link3,link4,";
$tags = rtrim($tags, ","); // remove last comma
$tags = explode(',', $tags); // turns to array
foreach($tags as $tag){ // iterate 
    $arr[] = "<a href='$tag'>$tag<a>"; //turn it into link
}
$final = implode(",",$arr); //turn it back to string
echo $final;