Ignore last data explode is taking? PHP

explode, php

Solution

You can use `trim($var, ',')` to remove the last comma when passing the string to the explode.

$var_explode = explode(',', trim($var, ','));

Problem

let's say my code is. ``` <?php $var = "1,2,3,4,5,6,"; $var_explode = explode(',', $var); foreach ($var_explode as $number) { echo "$number test"; } ?> ``` And when it echos, it goes like, 1 test, 2 test, 3 test, 4 test, 5 test, 6 test, test. The last one is un-needed, I know its caused because I have a comma after the 6 in my variable, but I need that comma there, not going to remove it. Thanks!

Original source