0

This may be dumb but I've been beating my head against it for a bit now..

$values contains this text:

'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest',

Why won't this trim the last comma?

trim($values, ",");

EDIT - this is how $values is being generated:

$values = "";
foreach($users_table as $k=>$v){ $values .= "'$v',"; }
trim($values, ",");
doub1ejack
  • 10,627
  • 20
  • 66
  • 125

4 Answers4

2

Is it possible that there's extra whitespace after the end of your string? Trying printing it surrounded with quotes.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
0

this works for me

<?php
$str = "'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest',";
echo( trim($str, ","));

?>

you could also do this

echo( trim(trim($str, " "),","));

or

echo(trim($str, ", "));

if you just want to put the values of an array in to a new array just use array_values()

$values = array_values($users_table);
echo($values);
mcgrailm
  • 17,469
  • 22
  • 83
  • 129
  • It doesn't seem possible for this string to have any trailing whitespace (see my edit to the original post). But I tried this to be on the safe side and didn't have any luck. – doub1ejack May 13 '11 at 13:55
  • @sing1ejack I edited my answer let me know if that does what you need – mcgrailm May 13 '11 at 15:58
0

You probably have some extra character after last comma ,. See this code:

$str = "'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest', ";
var_dump(trim($str, " "));

OUTPUT

string(69) "'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest',"

Try checking your original string with var_dump like this:

var_dump($str);
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

I caved.

$values = preg_replace("/,$/", "", $values);

If anyone knows what I was doing wrong, I'd love to hear it though. It's got me stumped.

doub1ejack
  • 10,627
  • 20
  • 66
  • 125