$string = rtrim($string, ',');
Docs for rtrim here
Top answer 1 of 10
348
$string = rtrim($string, ',');
Docs for rtrim here
2 of 10
48
This is a classic question, with two solutions. If you want to remove exactly one comma, which may or may not be there, use:
if (substr($string, -1, 1) == ',')
{
$string = substr($string, 0, -1);
}
If you want to remove all commas from the end of a line use the simpler:
$string = rtrim($string, ',');
The rtrim function (and corresponding ltrim for left trim) is very useful as you can specify a range of characters to remove, i.e. to remove commas and trailing whitespace you would write:
$string = rtrim($string, ", \t\n");
PHP
php.net › manual › en › function.rtrim.php
PHP: rtrim - Manual
This shows how rtrim works when using the optional charlist parameter: rtrim reads a character, one at a time, from the optional charlist parameter and compares it to the end of the str string. If the characters match, it trims it off and starts over again, looking at the "new" last character in the str string and compares it to the first character in the charlist again.
HashBangCode
hashbangcode.com › article › delete-trailing-commas-php
Delete Trailing Commas In PHP | #! code
This function uses a regular expression to match for one or more commas or spaces after the main bulk of text and before the end of the string and prints out the main bulk of text.
TutorialsPoint
tutorialspoint.com › remove-comma-from-a-string-in-php
Remove comma from a string in PHP?
October 13, 2020 - You can also replace commas with spaces or other characters ? <?php $numbers = "1,2,3,4,5"; $spaced = str_replace(',', ' ', $numbers); echo "With commas: " . $numbers . "<br>"; echo "With spaces: " .
Kirby Forum
forum.getkirby.com › the cms › questions
Trim a trailing comma - Questions - Kirby Forum
April 30, 2015 - hey all, I display my tags using this code: , The tags show up in a row separated by commas. The trouble now is that behind the last tag there’s still a comma. I know somehow i can trim a comma off a string by using rtrim but I can’t seem to find a way to integrate it into my code. will ...
SitePoint
sitepoint.com › php
Remove the Last Comma - PHP - SitePoint Forums | Web Development & Design Community
July 3, 2013 - Hi there, I have a query which will return a list of the items where I have added the comma to separate the string like this item1, item2, item3, . I wish to know is there any methods I could use to remove the last com…
W3Schools
w3schools.com › php › func_string_rtrim.asp
PHP rtrim() Function
<!DOCTYPE html> <html> <body> Without rtrim: Hello World! <br>With rtrim: Hello World! </body> </html> ... <?php $str = "Hello World!\n\n\n"; echo "Without rtrim: " . $str; echo "<br>"; echo "With rtrim: " .
PHP Developers Network
devnetwork.net › board index › programming › php - code
Trimming all white spaces and trailing comma - PHP Developers Network
August 16, 2012 - Explode on Commas array_map trim Loop through array, unset non-integer||invalid entries Done. ... <?php $string = "1,2,a, 5,6 ,7,"; $array = array_map('trim', explode(',', $string)); foreach ($array as $k => $v) { if (!preg_match('#^\d+$#', $v)) { unset($array[$k]); } } $string = implode(',', $array); echo $string;
Top answer 1 of 3
3
You have to assign the rtrim to $sql like that:
$sql = trim($sql, ",");
2 of 3
1
Better way is to use implode(). Example:
$array = array("top" => array("one" => "inner one", "two" => "inner two"));
foreach($array as $key=>$val){
$form = array();
$sql = "INSERT INTO $key SET ";
foreach ($val as $k=>$field) {
array_push($form, $k."='".$field."'");
}
$sql .= implode(",",$form);
//echo $sql;
}
Output:
INSERT INTO top SET one='inner one',two='inner two'
Tutorialsbites
tutorialsbites.com › php-remove-last-character-from-string-if-comma
In PHP remove last character from string if comma
June 4, 2022 - What is Git? Git is a powerful software tool that helps people manage their work on projects, especially in computer programming and software development. Imagine you are writing a book… · There are few days left on the 1500 prize bond draw result, bringing excitement and hope for many.