in reply to Re: Counting SubStrings, Style Question
in thread Counting SubStrings, Style Question

> Another reason split is bad: If the string ends with the substring you get 1 less in split's result.

May I correct you?

Split isn't "bad" it's wrong!

Excellent, the best reason so far! =)

> I also find it more readable.

Well ... readability is a question of taste and habit. And this approach needs to always initialize $count with 0.

But IMHO the o modifier is quite pointless here not really necessary anymore and doesn't make it more readable. :)

> ...which is faster and uses less memory in my benchmarks than the =()= "operator".

I got a penalty between 50 and 100% which is not sooo dramatic... and this highly depends on the length of the investigated string!

So the question is rather, if searching for millions of matches is really a common use case of the OP.

Cheers Rolf

  • Comment on Re^2: Counting SubStrings, Style Question