Take the 2-minute tour ×

I am trying to set up a tagging system, but i am having a little diffuculty here, if someone could help i would be very appreciative, anyhow i am just trying to query the database for the tags which are stuck into a row in the database of a table, and each tag is seperated by a comma, well i am trying to take each of those tags and create a link out of each one.

once i query the database the output basically looks as follows:

tag1, tag2, tag3, tag4, tag5, tag6

and i want to be able to separate them.

share|improve this question
Wait, so these tags are in one cell not a whole row...How are those commas printing unless you make them print? –  Trevor Arjeski Mar 16 '11 at 3:02
I guess you are not looking for a way to tokenize the string: $tags = explode ( "," , "tag1, tag2, tag3, tag4, tag5, tag6"); May be you should be clear: [1] In what format the information is contained in db? [2] In what format do you want and in what kind of variable –  Sarwar Erfan Mar 16 '11 at 3:06
ah sorry, yeah it is stored exactly like that in the database, its a row named "tags". –  mcbeav Mar 16 '11 at 3:12
so after i do my query everything is stored in the variable $tags, so i should $tags = explode("," , $tags); and then i can call each one in a loop like $tags[1], $tags[2], etc etc, ? i was looking into implode, but i i knew that wasnt right. –  mcbeav Mar 16 '11 at 3:16

2 Answers 2

up vote 2 down vote accepted

If you already have a comma seperated value from the database, you can use a regex to print out the tags:

echo preg_replace( "/([a-z]+)/", "<a href=\"#$1\">$1</a>", $str ); It just goes through searching for a-z words and converts them in to links.

Some other options could be:

  • preg_replace_callback()
  • explode() (as the other user said)
share|improve this answer
thankyou, that is very useful, which in your opinion is more efficient, imploding the string, and using a loop or the way you presented? looks like there is less code there that you have listed –  mcbeav Mar 16 '11 at 3:18
It depends on what you're trying to do. Imploding a string is most easiest to understand for the programmer and if there aren't too many tags, it isn't very resource intensive... However, regexes might (unsure) be more resource efficient, but I'm assuming it is. –  Ralph Wiggum Mar 16 '11 at 3:23
thanks man, the only thing i am not sure i understand in the code that you presented is the "<a href=\"#$1\">$1</a>" i am assuming that $1 you used to represent a tag or tag number one, and the actual link is just a hash with the variable contents printed inside of it, and the slashes are escape characters. am i understanding that? –  mcbeav Mar 16 '11 at 3:25
well, the code works like a charm, but the only issue is, that if there is a number in the tag, the number is not a part of the link. kind of odd, but i guess its looking for words listed a to z can i add numbers to that? –  mcbeav Mar 16 '11 at 3:27
Change it to [a-z0-9] –  Ralph Wiggum Mar 16 '11 at 3:51

This is a very good way of implementing tagging system.


other way is using explode()

share|improve this answer
thanks a ton for the tutorial, def, some good information! –  mcbeav Mar 16 '11 at 3:19

Your Answer


