# Working with Strings

Web @ 06 July 2006

To make string separated by commas I needed to make loop, but there is last comma which i do not need, so I used PHP function substr(). In my sample there was MySQL query and i was fetching record by record and writing values to string:

//$kiek – mysql_num_rows returned value
//$rezult – some mysql query’s result
for($i = 0;$i < $kiek; $i++) {
   //get array of values
   $found = mysql_fetch_array($rezult);
   //add record’s value to $record_str, plus comma
   $record_str .= $found[record] . ", ";
}
//remove last three symbols from string to get rid of last comma
substr($record_str, 0, sizeof($record_str)-3)
 

So finaly I got my needed string:

value1, value2, value3, …, valueN

Instead of:

value1, value2, value3, …, valueN,

Leave a Reply

Spam protection by WP Captcha-Free