while ($row = mysql_fetch_array($result)) { $tags = explode(', ', $row['tags']); foreach($tags as $b) { if(count($tags) >= 3 ) { echo '', $b, ' '; } else { echo "$b "; } } }