Forum Moderators: coopster
eregi(pattern,string) [OR] stristr(string,string)
It's intended for matching multiple parts of user agent names (in an array) against $_SERVER['HTTP_USER_AGENT']
For example:
$agent = $_SERVER['HTTP_USER_AGENT'];
$block = array('asp search','websearch','etcetera');
$blocked=false;for($i=0; $i<count($block); $i++)
{
// EITHER THIS:
if( eregi("/$block[$i]/i",$agent) ) {
$blocked=true; break;
}
// ... OR:
if( stristr($block[$i],$agent) ) {
$blocked=true; break;
}
}
Anyone else is is using either eregi or stristr? Which is faster?
The blocked bots list (parts of their names) which I just created is not too long, it's: "asp search", "Alexibot", "Bullseye", "CherryPicker", "Collector","Copier", "Crescent", "Download", "Email", "Extractor", "Grabber", "Harvest", "Leacher", "Mechanic", "Mozilla/2", "MSIECrawler", "MSProxy", "NICErsPRO", "Offline", "Openfind", "psbot", "Teleport", "Telesoft", "Bandit", "WebEMailExtrac", "WebFetch", "websearch", "Webster", "WebViewer", "WebZIP", "Widow", "Wget", "Zeus"
My question remains.. which is faster: eregi [or] stristr?
Also strpos is a bit faster. strstr has to do some memory allocation & string building to return its result; it is basically a combination of strpos and substr.
$text = 'blagga foo bar';
$part = [blue]strstr[/blue]($text, 'foo');
$part = substr($text, [blue]strpos[/blue]($text, 'foo'));
1. using strstr (first do strtolower) instead of stristr in an iteration, is obviously faster
2. strstr is faster than strpos, although the average difference is very small
I am doing about 40 iterations max. on user agents (see above), so I'm now using:
$agent = strtolower($_SERVER['USER_AGENT']);
$banned_list = array('one','two','etc.');for($i=0; $i<count($banned_list); $i++)
{
if( strstr($agent,$banned_list[$i]) ) { $banned=true; break; }
}
2. strstr is faster than strpos, although the average difference is very small