I have a string which will contain a file upload name, for example "image.jpg". I'm trying to use the explode function but it's returning an error "explode() expects parameter 2 to be string, array given in..."
I've tried looking for reasons why and comparing it to how use is instructed on PHP.Net but to no avail.
$upload_extension = end(explode(".", $feature_icon));
I probably should know better than to reply to a years old thread, but for what it's worth:
$upload_extension = end(explode(".", $feature_icon));
(which doesn't work because end() can only accept arrays defined in a variable, not those returned by functions) can be replaced with:
$upload_extension = explode ('.', $feature_icon) [count (explode ('.', $feature_icon)) - 1];
Whether or not you find that more or less elegant than using an intermediary variable to store the array, or using two lines of code (both as suggested above) is a matter of personal preference.
you can not use end()
like you are doing since
end()
-> Parameters ¶ The array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.
so do like
$feature_icon ="image.jpg";
$upload_extension = explode(".", $feature_icon);
$upload_extension = end($upload_extension);
var_dump($upload_extension );
Live result
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