Forum Moderators: coopster

Message Too Old, No Replies

How would I do this? - string within a string

find out whether a string contains another substring

         

electricocean

12:14 am on Jun 29, 2005 (gmt 0)

10+ Year Member



Hi,

Is there a way to see if a string contains something?

$a = "My friend is cool";
if($a contains 'is'){
echo "True!"
}
else{
echo "False!";
}

...and so on.

Csn I do this?

electricocean

Dijkgraaf

12:22 am on Jun 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if (preg_match('is', $a))

<added>Note this will match anything with 'is' in it not just the word is. So if you want just the word is you will have to use a regular expression.
</added>

JamShady

12:35 am on Jun 29, 2005 (gmt 0)

10+ Year Member



substr_count() would be much faster, unless you actually want to check for the word is (as opposed to 'is' in This for example), in which case you'd do something like preg_match('#\Wis\W#', $str);

coopster

1:17 pm on Jun 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



As noted, there are quite a few different ways to accomplish the task. However, in this particular case, strpos() [php.net] will be much faster and less memory intensive. But don't just take my word for it [php.net] ;)

electricocean

10:10 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



Well I am trying to test which browser they are using.

$browser = $_SERVER['HTTP_USER_AGENT'];
$veiw1 = strpos($browser, 'MSIE');
$view2 = strpos($browser, 'Mac');
if($view1 === true && $view2 === true){//if the browser is IE mac
echo "Please use a diffent broser";
}
else{//if it's not
echo "";
}

but this doesn't work... any help?

thanks
electricocean

coopster

10:56 pm on Jun 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you considered get_browser [php.net]?

electricocean

11:51 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



I don't really understand it....

coopster

8:27 pm on Jun 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There is example code on the get_browser() link that you could play around with. The problem you had with your original code there though is probably a typo, if you had error_reporting on you would notice that $view1 is a variable that does not exist ...
$browser = $_SERVER['HTTP_USER_AGENT']; 
$veiw1 = strpos($browser, 'MSIE');
$view2 = strpos($browser, 'Mac');
if($view1 === true && $view2 === true){//if the browser is IE mac
echo "Please use a diffent broser";
}
else{//if it's not
echo "";
}

electricocean

11:45 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



well i changed $veiw1 to $view1 and still nothing happend. So I tried get_browser.

My Code:

$view = get_browser(null, true);
if($view[browser] == 'Mac MSIE'){//Stop IE MAC
} else{//All other browsers are lovable
}

Output:

Warning: get_browser(): browscap ini directive not set. in /usr/export/www/hosting/dkicks/index.php on line 28

Is there a way I can change the settings or something?

thanks,
electricocean

coopster

11:54 pm on Jun 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you want to use get_browser() you have to load a browscap.ini file -- the directions are in the link provided.

Your other code here works for me as long as I remove the 'Mac' part of it as I'm not using a Mac. Have you tried dumping the HTTP_USER_AGENT $_SERVER variable to your screen first to see what is in it?

electricocean

12:15 am on Jul 1, 2005 (gmt 0)

10+ Year Member



I have looked inside it but if I only say MSIE or or something it will block all of IE but only IE mac to be not able to view stuff....

coopster

12:22 am on Jul 1, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, the logic in the code works without error. How do you know it is not working? Are you testing it with a Mac?

electricocean

5:00 am on Jul 1, 2005 (gmt 0)

10+ Year Member



Yes, I am using a mac. When i use IE the flash file is still being loaded (but can't open it, that is why i ned to ban IE mac) when I told the script not to.

I am very confused,

electricocean

coopster

12:52 pm on Jul 1, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I am too. Which code are you using? If you are using the first version, your issue probably lies in exact equality. Exact meaning that the strpos value returned has to actually evaluate to boolean true since you have the triple equal signs (===).

If this is the code you are using, you need to change it to equality without type comparison as strpos will return the first position that the string starts in, which will be a number, not boolean true. Note the double equal signs instead (==) ...

if($view1 == true && $view2 == true){//if the browser is IE mac

Resource:
[php.net...]

electricocean

6:36 am on Jul 2, 2005 (gmt 0)

10+ Year Member



Ok. Now I changed the '===' to '==' and still it doesn't work. I think IE mac is doing something wierd. IE mac still can't load the flash file which really pisses me off. Well I'm off to bed.

electricocean