how do you test is a character exists in a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

how do you test is a character exists in a string

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post by s.dot »

Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

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

Post 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 ?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

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

Post 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).
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

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

Post by scheinarts »

hey thanks man, i appreciate the explanation !
Post Reply