tags:

views:

81

answers:

5

I'm still new to PHP and I couldn't find this in the manual because I'm not sure of the name of the function. I have an array with These values:

$files[] = 

[0] => 01_Nhemamusasa.mp3 
[1] => 02_Kari_Mudande.mp3 
[2] => 03_Chikende.mp3 
[3] => 04_Karinge_Zuva.mp3 
[4] => 05_Mbirimo.mp3 
[5] => 06_Muchenjedza_Mutonga.mp3 
[6] => 07_Skokianna.mp3 
[7] => 08_Mbavarira.mp3

What I need to do is truncate the contents to get rid of the part before the _ and get rid of the filetype. I'd like to also convert the underscores found in the middle of the Arrays [1] and [5] (in this example) to become spaces. In other words, I need to convert these file names to be an array of the actual names of the songs.

Thank for any help!

Edit: My host is using PHP 5.2.12

+2  A: 

You will want to use the array_walk function: http://us3.php.net/array_walk If you are using PHP 5.3, you can pass array_walk an anonymous function.

EDIT: array_map will also do the trick as well. Same advice about anonymous functions apply here as well.

wilmoore
You can use lambdas with PHP < 5.3 when using `create_function`
Gordon
@Gordon: This is certainly true and since you brought it up, it should also be noted that the caveat regarding create_function is that you will likely end up escaping the code. Many developers find this ugly and would rather write a one-off class/method and pass it through via an array [ array('class', 'method') ].
wilmoore
@wilmoore yeah, it aint pretty with create_function and it's slow as well.
Gordon
+3  A: 

Using combination of array_map(), preg_replace() and str_replace():

$array = array(...);

$array = array_map(function($item) {
    return str_replace('_', ' ', preg_replace('#^\d+_(.*?)\.[a-z0-9]{2,6}$#i', '$1', $item));
}, $array);

print_r($array);
Crozin
it should be noted that this requires PHP5.3+
Gordon
A: 

You can use foreach to iterate the array and use substr to remove the first three chars, again for the last four or each string.

After that you can use str_replace to change the underscore with a space.

Adirael
+1  A: 
$input = array(
    '01_Nhemamusasa.mp3',
    '02_Kari_Mudande.mp3',
    '03_Chikende.mp3',
    '04_Karinge_Zuva.mp3',
    '05_Mbirimo.mp3',
    '06_Muchenjedza_Mutonga.mp3',
    '07_Skokianna.mp3',
    '08_Mbavarira.mp3'
);

function convert($item) {
    $item = explode('_', $item);
    unset($item[0]);
    $item = implode('_', $item);
    $item = explode('.', $item);
    $item = str_replace('_', ' ', $item[0]);
    return $item;
}

$output = array_map('convert', $input);

print_r($output);

Output:

Array
(
    [0] => Nhemamusasa
    [1] => Kari Mudande
    [2] => Chikende
    [3] => Karinge Zuva
    [4] => Mbirimo
    [5] => Muchenjedza Mutonga
    [6] => Skokianna
    [7] => Mbavarira
)
Edward Mazur
it should be noted that this requires PHP5.3+
Gordon
Looks great, but my host is running 5.2.12 :(
Joel
@Gordon I assume the inline function was the part that made this require 5.3+?@Joel I reworked the code to not use an inline function, so you should be able to use it now.
Edward Mazur
Thanks Edward! Will take a try. I appreciate your help!
Joel
This worked great. Thanks again!
Joel
A: 

both php string replacement functions (str/preg_replace) accept arrays as the "subject" argument:

$files = preg_replace('~^\d+ (.+?)\..+$~', '$1', 
    str_replace('_', ' ', $files));
stereofrog