111

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,030
  • 21
  • 100
  • 124
JoJo
  • 1,313
  • 2
  • 11
  • 12

11 Answers11

252

Use the rtrim function:

rtrim($my_string, ',');

The Second parameter indicates the character to be deleted.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Ander2
  • 5,341
  • 2
  • 22
  • 42
28

Try:

$string = "'name', 'name2', 'name3',";
$string = rtrim($string,',');
Boaz
  • 18,950
  • 8
  • 62
  • 67
  • 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
15

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,030
  • 21
  • 100
  • 124
VijayS91
  • 1,515
  • 23
  • 38
11

You can use substr function to remove this.

$t_string = "'test1', 'test2', 'test3',";
echo substr($t_string, 0, -1);
Hassaan
  • 6,770
  • 5
  • 29
  • 47
Annie Chandel
  • 205
  • 3
  • 3
7

rtrim function

rtrim($my_string,',');

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

Boaz
  • 18,950
  • 8
  • 62
  • 67
Jitendra Tyagi
  • 153
  • 1
  • 6
5

use rtrim()

rtrim($string,',');
Sankalp Mishra
  • 5,766
  • 4
  • 28
  • 58
5

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
  • 142
  • 3
  • 13
4

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);
Ashok Khot
  • 445
  • 4
  • 15
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
  • 4,216
  • 2
  • 32
  • 36
2

its as simple as:

$commaseparated_string = name,name2,name3,;
$result = rtrim($commaseparated_string,',');
Ketan Savaliya
  • 1,000
  • 8
  • 10
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
  • 5,878
  • 4
  • 26
  • 29