Filter-explode · 15 October 2007, 12:43

Browsing the explode() function reference on PHP.net, I came across a note giving a helpful tip about avoiding having empty elements in your result array, due to multiple (and an unknown amount of) delimiters splitting your string. More tangibly: the example was trying to extract words by removing spaces.

In particular, the subject of the note looked something like this:

$subject = "This string has too many spaces";

Now, explode() will not return an array containing the legible words, but there will be plenty of empty elements between them. This is because explode() is pedantic in its search for delimiters, and treats empty strings as important information. It can be – and it often is. But in just as many cases, you’ll want to discard those elements.

The suggested alternative?

preg_split ("/\s+/", $subject);

Looped 10 000 times, this took 0.167647123337 seconds to execute. That’s not bad at all, but, as I said, occasionally, I’m persuaded to explore. I came up with this:

array_filter(explode(" ", $subject));

Looped 10 000 times, this took 0.0909039974213 seconds to execute – execution time was nearly halved!

Of course, the resulting array has ‘holes’ in it’s key sequence. I’m not one of the people who bother with these, since I think it’s bad practise to use for instead of foreach where it can be avoided (and, truth be told, I can’t come up with a situation where it couldn’t be), but for those who care:

array_values(array_filter(explode(" ", $subject)));

Looped 10 000 times, this gave me a figure of 0.109376907349 seconds – still significantly less than it’s preg_split() alternative.

Perhaps one day when I have time, I’ll try benchmarking the methods with longer strings in both or either delimiter and subject, to see how that compares. But for the typical use of explode() – e.g. to split comma-separated values (but not the CSV format in the strictest sense – explode() has other issues there) – filter-explode should be the better and more legible alternative.

Ah.

Excellent.

I’ve now done my good deed for the day.

— Neike Taika-Tessaro

---

Comment

Textile Help
Categories: development , knowledge , php
Related:

Beating a dead horse: Jurassic Park raptors · 27 December 2008, 03:23

mIRC Script: Opping yourself in all channels in a single go · 14 December 2008, 21:48

mIRC Script: Flexible moderation · 14 December 2008, 21:27

mIRC cut script · 8 December 2008, 09:22

Global variables in PHP · 17 October 2007, 17:35