note 104127 deleted from function.preg-match by cmb
| From: | cmb@php.net | Date: | Sun, 04 Dec 2016 12:05:42 +0000 |
| Subject: | note 104127 deleted from function.preg-match by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208180@lists.php.net to get a copy of this message | ||
Note Submitter: juanmadss at gmail dot com
----
Testing the speed of preg_match against stripos doing insensitive case search in strings:
<?php
$string = "Hey, how are you? I'm a string.";
// PCRE
$start = microtime(true);
for ($i = 1; $i < 10000000; $i++) {
$bool = preg_match('/you/i', $string);
}
$end = microtime(true);
$pcre_lasted = $end - $start; // 8.3078360557556
// Stripos, we believe in you
$start = microtime(true);
for ($i = 1; $i < 10000000; $i++) {
$bool = stripos($string, 'you') !== false;
}
$end = microtime(true);
$stripos_lasted = $end - $start; // 6.0306038856506
echo "Preg_match lasted: {$pcre_lasted}<br />Stripos lasted: {$stripos_lasted}";
?>
So unless you really need to test a string against a regular expression, always use strpos / stripos
and other string functions to find characters and strings within other strings.