10

I have a comma separated string, which consists of a list of tags and want to convert it to array to get a link for every tag.

Example:

$string = 'html,css,php,mysql,javascript';

I want to make it like this:

<a href="tag/html">html</a>, <a href="tag/css">css</a>, <a href="tag/php">php</a>, <a href="tag/mysql">mysql</a>, <a href="tag/javascript">javascript</a>

So the result will be a string containing comma separated links with a space after each link and with no comma after the last link.

I have this function where $arg = 'html,css,php,mysql,javascript':

function info_get_tags( $arg ) {
    global $u;

    $tagss = '';
    if ( $arg == '' ) {
        return '';
    } else {
        $tags_arr = explode( ',' , $arg );
        foreach ( $tags_arr as $tag ) {
            $tags = '<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag . '</a>';
            $tagss .= $tags;
        }

        return $tagss;
    }
}

This script works for me but without commas and spaces and if we add a comma and a space here:

$tags = '<a href="' . $u . 'tag/' . $tag . '/">' . $tag . '</a>, ';

we get commas and spaces but there will be a trailing comma after the last link.

0

10 Answers 10

17

Just like you exploded you can implode again:

$tags = explode(',', $arg);
foreach ($tags as &$tag) {
    $tag = '<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag . '</a>';
}

return implode(', ', $tags);
4
  • 1
    this works well for me but would you tell me what's the & before $tag for? Commented Apr 30, 2011 at 23:06
  • 2
    @med: The & means that you iterate by reference. That way you can modify $tag and the element in the $tags array will be modified, too. Commented Apr 30, 2011 at 23:07
  • It's an operator to pass by reference, which means it's directly modifying the original value and not a copy of it. Commented Apr 30, 2011 at 23:09
  • You should always unset the var you have just references within a foreach: foreach ( $a as &$b ) { ... unset($b); } It's somewher on PHP.net and a very good practice. Commented Apr 30, 2011 at 23:53
2

Here's an alternative that uses array_map instead of the foreach loop:

global $u; 
function add_html($tag){
    return('<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag .  '</a>');
}
function get_tags($taglist){
    $tags_arr = array_map("add_html", explode( ',' , $taglist));
    return implode(", " , $tags_arr);
} 
1

Try this short code

$string = 'html,css,php,mysql,javascript';

$string = explode(',', $string);
   foreach( $string as $link){
   echo '<a href="tag/'.$link.'">'.$link.'</a>';
}

0

Easiest way is to the html into an array (each tag link is an array element) and then implode on ,...

if ( $arg == '' ) {

    return '';

} else {

    $tags_arr = explode( ',' , $arg );
    $tags  = array();
    $tagtpl = '<a href="%s" title="%s">%s</a>';

    foreach ( $tags_arr as $tag ) {
        $url = $u . 'tag/' . $tag . '/';
        $tags[] = sprintf($tagtpl, $url, $tag, $tag);

    }

    return implode(', ', $tags);

}
0

Try this

$tagss = trim($tagss);    
return substr($tagss, 0 , strlen($tagss)-1);
2
  • You can just write substr($tagss, 0, -2) ;) Commented Apr 30, 2011 at 22:56
  • @nikic yeap you're right, faster and nicer, but rtrim is even better Commented Apr 30, 2011 at 23:01
0

Why not put all tags in an array when you are creating them, and later explode the array and add the commas and spaces between the tags.

foreach ( $tags_arr as $tag ) {
    $tags = '<a href="' . $u . 'tag/' . $tag . '/" title="' . $tag . '">' . $tag . '</a>';
    $tagss[] = $tags;
}
$tagss = explode(', ', $tagss);
0

The workaround(!) would be to remove the unwanted trailing characters afterwards:

$tagss = rtrim($tagss, ", ");

This rtrim removes any mix of spaces and commas from the right end of the string.

Btw, you could use str_getcsv instead of explode, which also handles input spaces better.

0
0
function info_get_tags($arg)
{
    global $u;
    if (empty($arg)) return '';
    return ltrim(preg_replace('/([^\,]+)/', ' <a href="' . $u . '/${1}/" title="${1}">${1}</a>', $arg));
}
0
$string = 'html,css,php,mysql,javascript';

puts $string.split(/,/).map { |tag| "<a href=\"tag/#{tag}\">#{tag}</a>"}.join(", ")

result:

<a href="tag/html">html</a>, <a href="tag/css">css</a>, <a href="tag/php">php</a>, <a href="tag/mysql">mysql</a>, <a href="tag/javascript">javascript</a>
-3

Another solution:

$html = trim(preg_replace('/([^,]+)/', ' <a href="/tags/\1" title="\1">\1</a>', $string));

Or if you have to html encode the tags (always smart, since you're creating html from text):

$html = trim(preg_replace_callback('/([^,]+)/', function($match) {
    $tag = $match[1];
    return ' <a href="/tags/' . urlencode($tag) . '" title="' . htmlspecialchars($tag) . '">' . htmlspecialchars($tag) . '</a>';
}, $string));

So this $string would work too: "tag with space,html,css,php,mysql,javascript"

More regex is always good!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.