note 104127 added to function.preg-match
| From: | juanmadss at gmail dot com | Date: | Wed, 25 May 2011 11:00:45 +0000 |
| Subject: | note 104127 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180014@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 80.64.47.13
Probable Submitter: 87.216.160.9
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- https://master.php.net/note/edit/104127
Del: integrated -- https://master.php.net/note/delete/104127/integrated
Del: useless -- https://master.php.net/note/delete/104127/useless
Del: bad code -- https://master.php.net/note/delete/104127/bad+code
Del: spam -- https://master.php.net/note/delete/104127/spam
Del: non-english -- https://master.php.net/note/delete/104127/non-english
Del: in docs -- https://master.php.net/note/delete/104127/in+docs
Del: other reasons-- https://master.php.net/note/delete/104127
Reject -- https://master.php.net/note/reject/104127
Search -- https://master.php.net/manage/user-notes.php