¿Cómo eliminar la última coma de la cadena usando php?


Hola estoy usando loop para obtener valores de mi base de datos y mi resultado es como:

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

Y lo quiero así:

'name', 'name2', 'name3'

Quiero eliminar la coma después del último valor del bucle.

Gracias

Author: Rikesh, 2013-03-14

9 answers

Uso rtrim función

rtrim($my_string,',');

El segundo parámetro indica el carácter a borrar.

 141
Author: Ander2,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-20 14:09:59

Intenta:

$string = "'name', 'name2', 'name3',";
$string = rtrim($string,',');
 21
Author: Boaz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-14 12:52:59

Intente Debajo del Código:

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

Utilice este código para eliminar el último carácter de la cadena.

 9
Author: VijayS91,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-27 16:16:46

rtrim función

rtrim($my_string,',');

El segundo parámetro indica que la coma debe eliminarse del lado derecho.

 6
Author: Jitendra Tyagi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-14 12:52:16

Use rtrim()

rtrim($string,',');
 5
Author: Sankalp Mishra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-14 12:02:31

Puede usar la función substr para eliminar esto.

$t_string = "'test1', 'test2', 'test3',";
echo substr($t_string, 0, -1);
 5
Author: Annie Chandel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-27 11:26:47

Afectará a tu script si trabajas con texto multibyte del que subestimas. Si este es el caso, recomiendo encarecidamente habilitar las funciones mb_* en su php.ini or do this ini_set("mbstring.func_overload", 2);

$string = "'test1', 'test2', 'test3',";
echo mb_substr($string, 0, -1);
 3
Author: Ashok Khot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-19 09:27:38

Es tan simple como:

$commaseparated_string = name,name2,name3,;
$result = rtrim($commaseparated_string,',');
 1
Author: Ketan Savaliya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-09 11:44:16

Al principio lo intenté sin espacio rtrim($arraynama,","); y obtuve un resultado no válido.

Luego agregué un espacio y obtuve un resultado válido:

$newarraynama=rtrim($arraynama,", ");
 1
Author: Owais Alam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-28 11:17:23