View Full Version : detecting phrase in a string

06-08-2004, 10: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

06-08-2004, 10:21 PM

either strpos or strripos for case insensitive search.

06-09-2004, 02:51 AM

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

06-09-2004, 03: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.