¿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
9 answers
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,',');
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.
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.
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,',');
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);
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);
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,',');
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,", ");
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