124

I am using a loop to get values from my database and my result is like:

'name', 'name2', 'name3',

And I want it like this:

'name', 'name2', 'name3'

I want to remove the comma after the last value of the loop.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JoJo
  • 1,443
  • 2
  • 12
  • 12

12 Answers12

270

You may use the rtrim function. The following code will remove all trailing commas:

rtrim($my_string, ',');

The Second parameter indicates characters to be deleted.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Ander2
  • 5,569
  • 2
  • 23
  • 42
  • 39
    Also make sure you don't have any trailing space after the comma, otherwise this will fail, or do rtrim(trim($my_string), ','). – Foxhoundn Mar 03 '15 at 12:14
  • 3
    This doesn't work in a loop that adds a comma at the end as it will remove all the commas. – LizardKG Mar 11 '20 at 21:35
  • 1
    to expand on @Foxhoundn's answer you can use just use ```rtrim($my_string, ', ');``` with an added space in the second parameter if you have trailing space – IndexZero Dec 05 '22 at 04:46
30

Try:

$string = "'name', 'name2', 'name3',";
$string = rtrim($string,',');
Boaz
  • 19,892
  • 8
  • 62
  • 70
  • 1
    I already tried it but its showing like this: 'name''name2''name3' This is not what I need – JoJo Mar 14 '13 at 12:43
  • Per your example, you need to apply this function only on the final result and not on each value separately. – Boaz Mar 14 '13 at 12:47
  • I am using loop so how can I know the final result of loop ? – JoJo Mar 14 '13 at 13:56
  • The final result of the loop is stored in a variable, right? So apply the `rtrim` call to that variable. If you're still having difficulty, update your question with the code that produces the loop. – Boaz Mar 14 '13 at 14:00
  • I like this answer in particular, because one of the issues with a lot of PHP built-in functions, is that some of them are void, and some of them have return types. In this case, I was trying to just write `rtrim($string, ',')`, when I should be writing `$string = rtrim($string, ',');` – Alec Oct 14 '16 at 08:47
  • @Alec: Afaik, most of the [String functions](http://php.net/manual/en/ref.strings.php) have a return value. Void funcitons that directly modify the parameter can be for arrays or objects. (sorry a bit OT but can help memorize it!) – T30 Mar 20 '17 at 15:09
16

Try the below code:

$my_string = "'name', 'name2', 'name3',";
echo substr(trim($my_string), 0, -1);

Use this code to remove the last character of the string.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
VijayS91
  • 1,535
  • 23
  • 38
11

You can use substr function to remove this.

$t_string = "'test1', 'test2', 'test3',";
echo substr($t_string, 0, -1);
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Annie Chandel
  • 197
  • 3
  • 3
7

rtrim function

rtrim($my_string,',');

Second parameter indicates that comma to be deleted from right side.

Boaz
  • 19,892
  • 8
  • 62
  • 70
Jitendra Tyagi
  • 173
  • 1
  • 6
6

It is better to use implode for that purpose. Implode is easy and awesome:

    $array = ['name1', 'name2', 'name3'];
    $str = implode(', ', $array);

Output:

    name1, name2, name3
khandaniel
  • 306
  • 4
  • 13
5

use rtrim()

rtrim($string,',');
Engineer
  • 5,911
  • 4
  • 31
  • 58
3

It will impact your script if you work with multi-byte text that you substring from. If this is the case, I higly recommend enabling mb_* functions in your php.ini or do this ini_set("mbstring.func_overload", 2);

$string = "'test1', 'test2', 'test3',";
echo mb_substr($string, 0, -1);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ashok Khot
  • 461
  • 4
  • 16
3

its as simple as:

$commaseparated_string = name,name2,name3,;
$result = rtrim($commaseparated_string,',');
Ketan Savaliya
  • 1,190
  • 9
  • 11
3

You can use one of the following technique to remove the last comma(,)

Solution1:

$string = "'name', 'name2', 'name3',";  // this is the full string or text.
$string = chop($string,",");            // remove the last character (,) and store the updated value in $string variable.
echo $string;                           // to print update string.

Solution 2:

$string = '10,20,30,';              // this is the full string or text.
$string = rtrim($string,',');
echo $string;                       // to print update string.

Solution 3:

 $string = "'name', 'name2', 'name3',";  // this is the full string or text.
 $string = substr($string , 0, -1);
 echo $string;  
Majbah Habib
  • 8,058
  • 3
  • 36
  • 38
0

Solutions to apply during a loop:

//1 - Using conditional:

$source = array (1,2,3);
$total = count($source);
    
$str = null;
    
for($i=0; $i <= $total; $i++){ 
        
    if($i < $total) {
        $str .= $i.',';
    }
    else {
        $str .= $i;
    }
}
    
echo $str; //0,1,2,3

//2 - Using rtrim:

$source = array (1,2,3);
$total = count($source);

$str = null;

for($i=0; $i <= $total; $i++){ 
    
        $str .= $i.',';
}

$str = substr($str,0,strlen($str)-1);
echo $str; //0,1,2,3
Fellipe Sanches
  • 7,395
  • 4
  • 32
  • 33
-1

I tried everithing and just this solve my problem

$string = '10,20,30,'; // this is the full string or text.

$string = trim(trim($string),','); //double trim function

echo $string; // to print update string.