preg_match() vs strpos() for match finding?

I would prefer the strpos over preg_match, because regexes are generally more expensive to execute.

According to the official php docs for preg_match:

Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.


When in doubt, benchmark!

Obviously we could have come up with a better benchmark than this, but just to prove the point that as it starts to scale up, strpos() is going to be quite a bit faster. (almost 2x as fast here)

EDIT I later noticed that the regex was case-insensitive. When running this again using stripos() for a more fair comparison, the result is 11 to 15, so the gap narrows but preg_match() remains a lot slower.

$str = "the quick brown fox";
$start1 = time();
for ($i = 0; $i<10000000; $i++)
{
    if (strpos($str, 'fox') !== false)
    {
        //
    }
}
$end1 = time();
echo $end1 - $start1 . "\n";

$start2 = time();
for ($i = 0; $i<10000000; $i++)
{
    if (preg_match('/fox/i', $str))
    {
        //
    }
}
$end2 = time();
echo $end2 - $start2;

// Results:
strpos() = 8sec
preg_match() = 15sec

// Results both case-insensitive (stripos()):
stripos() = 11sec
preg_match() = 15sec