PHP : remove char.
- Started
- Last post
- 5 Responses
- tobor
How would you remove the last comma in this sentence:
id,tool,cert,
in php?
- unfittoprint0
is it the result of a for loop?
you could create an exception for the last item in the loop and avoid adding the comma...
- tobor0
yes, it is in a foreach loop... please show me how this 'exception' would work.
- unfittoprint0
//array
$a = array (1, 7, 8, 17);
foreach ($a as $v) {
if($i != count($a) ){
print "$v . ;";
} else {
print "$v ";
}
$i++;
}
- Pixter0
data = "id,tool,cert,";
data.split(",");
Bring a array with 4 elements, where the last is blankdata.pop();
Removes the last elementThem you have an array "data" with 3 elements (0, 1, 2)
I aways use that
- mike0
pixter's recommendation is nicer if you are using an array already, but if you have the value as a string and you just want to chop the last character then try this...
$sStr = substr(trim($sMyStr), 0, strlen(trim($sMyStr)) - 1);
that will give you the entire string back minus the last char and any spaces that may follow.
http://us3.php.net/manual/en/fun…
this is going to be a lot faster if the items are not in an array... otherwise, use pixter's suggestion... although his syntax is wrong...
$sStr = "bob, sue, tom, ";
$aStr = explode(", ", $sStr);
array_pop($aStr);
$sStr = implode(", ", $aStr);