...

View Full Version : detecting phrase in a string



sweenster
06-08-2004, 09:16 PM
am going to kill 2 birds with one stone here...

what PHP command could I use to detect a certain string inside another string?

example 1:
$browser = $_SERVER['USER_AGENT'];

how could i detect if $browser contains "MSIE"

example 2:
along the same lines but for detecting a key word or phrase in a paragraph

MrShed
06-08-2004, 09:21 PM
http://www.php.net/manual/en/function.strpos.php

either strpos or strripos for case insensitive search.

bcarl314
06-09-2004, 01:51 AM
Or...



if(preg_match("/MSIE/i",$_SERVER['USER_AGENT']) {
print "match";
}
else {
print "no match";
}

firepages
06-09-2004, 02:51 AM
... though mostly , if you can avoid regex do so (not just cos its harder :D) but as its much slower than normal string operations , if you have to use regex , use the preg_$func functions as per Bcarl's post as they are much faster than ereg() etc

but for a simple check like above (see MrSheds link)...



<?
if( strpos( $_SERVER['USER_AGENT'] , 'MSIE' ) !== false ){
echo 'IE';
}
?>


even though you could beef up the regex to check for several user-agents at once & then switch that result .. still in this case multiple strpos()'s will still be faster.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum