Replace parts of an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Replace parts of an array

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_filter() may be of interest.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply