PHP 5.3 split

slpit is deprecated in PHP 5.3, manual says

split() (use preg_split() instead)

explode is same as split ? Can’t i just replace slpit with explode ?

preg_split, since it is more advanced, support regular expression, do we need to use it for splitting a sentience into words ?

$string = "php is good";
$stringArray = explode(' ', $string);

or should i use preg_split as listed in php site ?

The manual says for split:

split — Split string into array by regular expression

So, making split deprecated and tell users to use preg_split instead makes total sense, since split() essentially was the same, but had a different name.

Explode is something different entirely since -as you said- it doesn’t use regular expressions.

If you just want to chop a string at each space I would stick with explode() if I were you; it should be faster than preg_split() for this purpose.

I ran a (small) benchmark to explode the first paragraph of Lorem Ipsum 10.000 times, once using explode() and once using preg_split()

These are the results:

explode: 0.287669897079 seconds
preg_split: 1.02503013611 seconds

So, explode() is about 4 times as fast as preg_split().

I benchmarked on a machine running PHP 5.2.6, but these results should be roughly the same for PHP 5.3.x

Benchmark code:

class Stopwatch
	private $_starttime;
	* Constructor
	function __construct() {}
	* Start the stopwatch
	public function start()
		$mtime = microtime();
  		list($usec, $sec) = split(' ', $mtime);
  		$this->_starttime = (float)$usec + (float)$sec;
   	* Return the current runtime (doesn't actually "stop" the stopwatch, but returns current running time, like "split time" on irl stopwatches)
   	* @return float Time the stopwatch has run (in seconds)
   	public function stop()
	   	$mtime = microtime();
   		list($usec, $sec) = split(' ', $mtime);
   		return (((float)$usec + (float)$sec) - (float)$this->_starttime);

$str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ultricies enim nec nisl tincidunt a ultricies metus ultrices. Cras nisi libero, posuere semper consequat id, mattis pretium diam. Sed porta ornare malesuada. Integer tempor lectus a risus consequat nec sodales nunc luctus. Mauris vel dolor massa. Pellentesque tempor sapien ut orci commodo luctus. Aliquam eleifend tempor suscipit. Proin in arcu velit. Fusce risus lorem, mattis et dignissim vitae, euismod vitae purus. Duis sed lectus sem, vitae eleifend tortor. Nulla interdum sagittis lacus sit amet varius. Proin consequat ultrices hendrerit. Quisque eget tortor a tortor facilisis hendrerit a sed ligula. Fusce vel ipsum erat, a rutrum felis. Etiam euismod fermentum tortor sit amet pharetra. Vestibulum placerat scelerisque ipsum eget posuere.';

$s = new Stopwatch;
for ($i=0; $i<10000;$i++)
$x = explode(' ', $str);
echo $s->stop();

echo '<br/>';

for ($i=0; $i<10000;$i++)
$x = preg_split('/\\s/', $str);
echo $s->stop();

Thank you very much for the detailed reply.

Even though i checked split manual few minutes ago, i don’t noticed it can handle regular expression.

This is not so smart kind of testing.
Usually you don’t have a code that runs 10.000 times, so, the difference is no more than 0.00001 sec, which is really insignificant and cannot be detected if your code does something more than just splitting a string.

It’s not a test, it’s a benchmark.
Using your reasoning I can always choose the slowest of mulitple possibilies, because it “doesn’t matter”.
Had I chosen the fastest method everywhere, as a whole my application would be faster, don’t you think?

Think of it this way: every optimization --however small-- is always welcome.

The test was performed to identify which function ran quicker. It had nothing to do with running something 10,000 times in a real world environment.

If you use functions across your application with a performance edge then these small savings mount up especially if its getting hit many times over.

No, they don’t.
None of ones, who really run an application with a performance edge, would advise such things.
Especially if its getting hit many times over :slight_smile:

Come again? What’s your point? Optimization is useless and noone should do it??

{face palm} :eek:

Sorry but I agree with Shrapnel_N5, it really doesn’t matter which is faster.

Sure, you should always use the fastest function but to claim that it actually has any real impact on how fast the page loads is rubbish. A single database query takes 1000x longer than running a function like explode or preg_split.

Surely best practice dictates that we write optimized code whether that be a small regex function or a whole class.

Actually, best practice dictates that you don’t optimize prematurely.

In this case, the difference between the two is so small that it’s not likely to make a difference in all but the biggest applications.

explode() and preg_split() are different in other ways if I’m not mistaken, as well.

I never claimed it would have a real impact on the page load.
I was just pointing out which of the two functions is faster.

While this is true, choosing explode() over split() because you don’t use regex --imho that’s the only difference between those 2 functions-- sounds like a good optimization to me.

Let me reverse the question: why should I use explode() if I can also use the slower preg_split() function?

That question doesn’t make sense right? So, reductio ad absurdum, I should use explode().

Just call it “good style”, not “optimization”. :slight_smile:
Of course proper tool should be always used. Explode is the best tool for the simple string splitting - so, it should be.
But nothing performance related here.
Just because you cannot measure the difference in the REAL environment.

Says who? Maybe the topic starter is looking for a way to count the number of unique words in a book by first applying either explode() or preg_split() to the full text of the book.
Still think you can’t measure the difference in this REAL environment as you put it? :wink:

BTW. I regret putting the speed difference in my earlier post. I was just pointing out one should use explode() if regex is not needed, that’s all :x

ScallioXTX, oh I missed your post :slight_smile:
To make it correct, Optimization is useful but almost noone understand it.

So you just went and assumed I don’t understand it either, or how am I to understand your post?

way to count the number of unique words in a book

They have no choice then. only regexp would help

So you just went and assumed I don’t understand it either

Most likely.
Because explode vs. preg_split argue is not of optimization.
There is no optimization without profiling.
You can refer to my post,
where I tried to explain some of these matters

How do you figure?