str_replace vs array_map & trim on URL segments Are there any advantages on using array_map &amp; trim vs using simple str_replace? The input for both is an array of matched URL segments. I find it nice to be able to provide extra logic when using array_map but seems to be ~2 times slower. I did some trivial benchmarking

for ($i = 0; $i &lt; 10000; $i ++) {
    array_map(function($argument) {
        return trim($argument, '/');
    }, array_slice($match, 1));
}

$end_time = microtime(TRUE);

echo $end_time - $start_time;

echo "&lt;br/&gt;";

$start_time = microtime(TRUE);

for ($i = 0; $i &lt; 10000; $i ++) {
    str_replace('/', '', array_slice($match, 1));
}

$end_time = microtime(TRUE);

echo $end_time - $start_time;


Outputs:

0.0675988197327
0.0296301841736
Which version to use

I wouldn't care about the timing difference. There may be situations where it matters, but in almost all cases, both variants will be so fast that the difference becomes irrelevant (we are talking about a fraction of a millisecond here) and other factors should decide which version you use.

In this case, those other factors might be readability and reusability/adaptability. I think the second version is more readable, but as you said, the first version can be easily adapted. 

So if you predict that you need to perform additional changes to the segments, go with the first approach, otherwise with the second.

Differences

Please note that the versions are not the same (the first one only trims slashes, while the second one removes them all). For your use-case, this doesn't matter. 

But as this code will probably be wrapped in a function which may then be reused in different contexts (you could eg pass the slash as argument, and then use the function to trim whitespace from arrays (for the first version), or you could use it to remove an unwanted character from the segments (for the second version)), it still matters.