10th July 2008 - 6 minutes read time
There are many ways to find a string within another string using PHP, but which one is the quickest? I did the same sort of test that I have done with double or single quotes and downloading web pages with a few of the string matching functions available in PHP.
I first generated a very long string that could be used in the string matching functions. The following for loop generates a string containing "iteration 0 and iteration 1 and iteration 2..." right up until "iteration 4999 and".
$testString = '';
for ($i=0; $i < 5000; $i++) {
$testString .= "iteration " . $i . " and ";
}
This was then passed to a number of string matching functions. This included the regular expression libraries contained in ereg() and preg_match(). The strpos() and strstr() functions were used as these are another common way of string matching, as well as some of their variants for matching case insensitive strings.