triming a string
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 = "";
foreac开发者_JAVA百科h($users_table as $k=>$v){ $values .= "'$v',"; }
trim($values, ",");
Is it possible that there's extra whitespace after the end of your string? Trying printing it surrounded with quotes.
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);
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.
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);
精彩评论