I'm having a few issues with the php explode function.
The string i want to explode is:
,.stl,.ppl
Currently, i'm using the explode function as such:
explode(',',',.stl,.ppl');
Unfortunately this is problematic, it returns three strings:
array(3) { [0]=> string(0) "" [1]=> string(4) ".stl" [2]=> string(4) ".ppl" }
Why is the first string blank?
The obvious solution is to skip the first element of the array, however, why do i need to do this?
Shouldn't the explode()
function automatically remove this blank array, or not even generate it at all?
This is normal behavior. You need to understand that the blank string is valid string value, hence PHP returns it.
It's quite helpful for cases where elements might not be there, to retain structure.
If you don't want it, you can simply filter it :
$array = array_filter( explode(",", $string ));
but do note that this will filter out anything that evaluates to false as well ( eg 0 ).
You can also trim the leading ',' in your string explode(',',trim(',.str,.ppl'),',')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With