Page 1 of 1

Replace parts of an array

Posted: Wed Jul 26, 2006 11:20 pm
by toasty2
Ok, I have a file listing script, and it saves all the files in an array called $file
I'd like it to remove .mp3 from the filenames when it displays them, I just need some code that will go through the array $file and replace each .mp3 part of the aray with nothing.

Posted: Wed Jul 26, 2006 11:24 pm
by feyd
array_filter() may be of interest.

Posted: Wed Jul 26, 2006 11:30 pm
by toasty2
I don't understand that function, it's weird. How would I replace for example in a array like this: "fred.mp3", "song.mp3" with "fred", song"? I can't manually change it because the content it generates is dynamic. (A directory Listing). So, how can I do that?

Posted: Wed Jul 26, 2006 11:33 pm
by feyd
Sorry, I understood you wanted to remove the entries, not just rename them. array_map() and array_walk() can be used for what you wish.

Posted: Wed Jul 26, 2006 11:44 pm
by toasty2
Thanks, none of your stuff worked. This worked:

Code: Select all

$newarray = str_replace(".mp3", "", $file);
I don't think you understood what I wanted to do, but I found out how myself. Thanks anyway.

Posted: Wed Jul 26, 2006 11:49 pm
by feyd
I understood, and they would work. However, they are intended for the leading edge instead of the trailing that yours is intended for. Either is fine in the end.