Welcome Guest, Not a member yet? Register   Sign In
howto make changes in string array
#1

[eluser]linderox[/eluser]
I have an array with filenames. I want to add some suffix before extension of the file, how can I do this? e.g. (file.jpg -> file_small.jpg)
#2

[eluser]Dam1an[/eluser]
I think the easiest way would be to use array_map


Code:
function add_suffix_to_filename($filename) {
  $parts = explode('.', $filename);
  $parts[size_of($parts-1)] = $parts[size_of($parts-1)].'_my_suffix';
  return implode('.', $parts);
};

array_map("add_suffix_to_filename", $files);

I'm sure you can clean it up a bit more, but it should give you an idea of what I'm on about
Basically, you call the function on each item in the array, and replace the value with the one produced by the function.
The function splits the filename based on . and add the suffix to the one before the extension

(I can see this causing problems with extensions such as .tar.gz etc)
#3

[eluser]xwero[/eluser]
i guess you want to rename them on the server, so i would use a loop
Code:
foreach($files as $file)
{
    $last_point = strrpos('.',$file);
    rename($file,substr($file,0,$last_point-1).'_my_suffix'.substr($file,$last_point));
}
I assume the files have a full path other wise you have to add it.
#4

[eluser]linderox[/eluser]
Oh!!! I need just deal with string no rename process, but I understand what to do. Thank you




Theme © iAndrew 2016 - Forum software by © MyBB