Page 1 of 1

how do you test is a character exists in a string

Posted: Thu Jan 24, 2008 7:43 pm
by scheinarts
Hi

I need to test if a certain character exists in a string, like spaces commas dots slashes etc

how do i do that in php

its basically the equivalent of javascript for indexOf() method

Thanks

Re: how do you test is a character exists in a string

Posted: Thu Jan 24, 2008 7:44 pm
by s.dot

Re: how do you test is a character exists in a string

Posted: Thu Jan 24, 2008 8:20 pm
by scheinarts
hey Thanks man.

What's up with this warning
Warning

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "".
so does it return false? or something else? because if it doesnt return false you can't use "!" can you ?

Re: how do you test is a character exists in a string

Posted: Thu Jan 24, 2008 8:24 pm
by Ambush Commander
Here's what the warning means: PHP is loosely typed, which means that 0 == FALSE. For strpos, this can be troublesome, since if you say if(strpos(..)) and it returns 0, the character DOES exist, but the if will evaluate as FALSE. To correctly check for it, state strpos(..) !== FALSE (notice the double equality).

Re: how do you test is a character exists in a string

Posted: Thu Jan 24, 2008 8:54 pm
by scheinarts
hey thanks man, i appreciate the explanation !