following does not happen?
(1000 iterations of both solutions).
Input:
$paragraph = "o'reilly and peter's dogs and cats";
$junk = ["'s",'/',',','.',';','-'];
Output of my solution:
Array
(
[0] => o'reilly
[1] => and
[2] => peter
[4] => dogs
[6] => cats
)
0.02592 secs
Output of your solution:
Array
(
[0] => o
[1] => reilly
[2] => and
[3] => peter
[4] => s
[5] => dogs
[6] => and
[7] => cats
)
10.00271 secs
In serveral cases your solutions is faster and not in the previous one. However the documentation says:
If you don't need the power of regular expressions, you can choose faster (albeit simpler) alternatives like explode() or str_split().
You must also be careful with: https://stackoverflow.com/questions/15137660/php-preg-split-utf8-characters
Finally, how to eliminate duplicate words with preg_split? I do this for my solution...
$duplicated = [];
$words = array_filter(
explode(' ',
str_replace($junk, ' ',$paragraph)),
function ($item) use (&$duplicated) {
if (trim($item) !== ""
&& !isset($duplicated[$item])) {
$duplicated[$item] = true;
return true;
}
return false;
});
Trim is better for clean chars like \n and performance is not greatly affected. But you can also add \n to $junk.
@being_void_xd
Why do you ping, when you can just pong
Simply you could give those chars instead of \PL to preg_split. Also that 10 seconds is bulls***. Try it here https://3v4l.org/2u5cd Using preg_split, in this case, is much more faster.
Обсуждают сегодня