Gex Posted July 11, 2010 Share Posted July 11, 2010 String FormattingHey, I just have been wondering. Having unsuccessfully (Or probably not looking hard enough) to find a way to format a string of words. What I wish to do is on my website I have the current users online. All the users online are in a list something like this. Player, Player, Player, Player,Now the problem I having, well not a problem really more of a cosmetic appearance to the overall look, what is happening is that when it outputs the data the last user online in the list has player, a comma at the end. Being the last in the list of online players I do not what a comma there. Is there a way to format the output so PHP knows when it has reach the end of a list. Thanks in advance.Gex Link to comment Share on other sites More sharing options...
Synook Posted July 11, 2010 Share Posted July 11, 2010 You could put them all in a list, then implode() said list. Or you could count along (using a variable) as you iterate through the names, and do something different when you come to the last one. Link to comment Share on other sites More sharing options...
HungryMind Posted July 11, 2010 Share Posted July 11, 2010 Try This Code:xxxxxxxxxxxxxxxxxxxxxxx<?php$Player = array("Player", "Player", "Player", "Player", "Player");$cntArray = count($Player);$Counter = 0;foreach($Player as $DisplayPlayer){ $Counter = $Counter + 1; print $DisplayPlayer; if ($cntArray != $Counter) { print ", "; } } ?>xxxxxxxxxxxxxxxxxxThnx & Regards: Hungry Mind (http://hungrymind.tk/) Link to comment Share on other sites More sharing options...
End User Posted July 11, 2010 Share Posted July 11, 2010 If they're in a simple list you could just do a trim() on the data and clip off the last comma before you display them:$data = trim($data, ','); Now the problem I having, well not a problem really more of a cosmetic appearance to the overall look, what is happening is that when it outputs the data the last user online in the list has player, a comma at the end. Being the last in the list of online players I do not what a comma there. Is there a way to format the output so PHP knows when it has reach the end of a list. Link to comment Share on other sites More sharing options...
Gex Posted July 11, 2010 Author Share Posted July 11, 2010 Thanks guys, i'll give them a shot and see what comes out. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.