3v4l.org

run code in 300+ PHP versions simultaneously
<?php function strpose_simple($haystack, $needle, $index = 0) { $hlen = strlen($haystack); $nlen = strlen($needle); do { $pos = strpos($haystack, $needle, $index); if ($pos !== false) { $end = $pos+$nlen; if (!isset($haystack[$pos-1]) || !trim($haystack[$pos-1])) { if (!isset($haystack[$end]) || !trim($haystack[$end])) { return $pos; } else $index = $pos+1; } else $index = $pos+1; } } while ($pos !== false && $pos < $hlen); return $pos; } function strpose_slow($haystack, $needle, $offset = 0) { $found = preg_match( '(\b(?:'.preg_quote($needle).')\b)Su', $haystack, $match, PREG_OFFSET_CAPTURE, $offset ); return ($found) ? $match[0][1] : FALSE; } $haystack = "Hello World"; $needles = array( "ll", "Hello", "Worlds" ); foreach (array('strpose_simple') as $function) { $begin = 0; $end = 200000; $start = microtime(true); while ($begin++<$end) { foreach ($needles as $id => $needle) { $function($haystack, $needle); } } printf("%s: %.4f seconds/%d peak\n", $function, microtime(true)-$start, memory_get_usage(true)); }

preferences:
34.86 ms | 402 KiB | 5 Q