Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using end() with explode() does not work

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));
like image 938
DorianHuxley Avatar asked May 11 '13 14:05

DorianHuxley


2 Answers

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.

like image 89
Frank van Wensveen Avatar answered Oct 01 '22 13:10

Frank van Wensveen


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

like image 27
NullPoiиteя Avatar answered Oct 01 '22 12:10

NullPoiиteя