Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] String delimiting in PHP

Started by Teenage Death Boy Jan 21, 2005 at 9:10 AM 1 replies 300+ views
Original Post
Teenage Death Boy
Teenage Death Boy
ok well I don't really know if delimiting is the right word for it, can't think at the moment. Basically, say I pulled 5 entries out of a database, each a single word. Now say I wanted to output them to the page as [word1, word2, word3, word4, word5] My first idea was obviously use a for loop. the leading and trailing [ and ] are simple, just stick them outside the loop, but the problem is getting the commas in. I can't stick them after the variable as that would result in the end looking like word5,] and I can't stick it before the variable as the beginning would look like [,word1 So my question is what would be the best way to seperate the variables with a comma. Is there a function that does this? I have it working at the moment, but I don't consider it very efficient, and its more of a 'hack'. Any help appreciated
twanvl
twanvl
You can use the function join for this. Assuming the 5 words are in an array:
$words = array('word1', 'word2' ,'word3', 'word4', 'word5');echo '[',  join(', ',$words), ']';

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.