Page 1 of 1

Searching a string for one word...

Posted: Mon May 19, 2003 7:12 am
by William_I
Okay... patience please... new to PHP...

Damn... PHP is nothing like Cold Fusion! :wink:


I'm looking to build a "restricted word" function into an online forum. When users post a message, I want to search the subject and message for a restricted word. For example, if it contains "foo" they're not allowed to post the message. (Long story on the reasons why)

What is the correct IF syntax to see if a string contains a particular word? In CF, it's IF #string CONTAINS "foo"... but I'm not finding the PHP equivalent.

-edit-

um... also... not to impose too much...


I'd also like to build in conditional logic that checks new usernames and restricts names to only numbers, characters a-z, and such. Is there a quick way in PHP to run this check?


Thanks.

Posted: Mon May 19, 2003 8:03 am
by Skittlewidth
You might be able to use strpos() :idea:

it's something like:

Code: Select all

if (strpos($posted_message, "foo") ===FALSE)  // checks the string for "foo"
{
//code to submit message 
}
else
{
echo "You cannot submit a message containing the word foo!";

}
Name validation is probably best done with Javascript

Skittlewidth 8)

Posted: Mon May 19, 2003 8:07 am
by twigletmac
Skittlewidth wrote:Name validation is probably best done with Javascript
Always good to validate server side though in case JavaScript is turned off:
http://php.net/ctype
and for more complex stuff or if the ctype functions are unavailable:
http://php.net/preg_match

Mac

Posted: Mon May 19, 2003 8:09 am
by Skittlewidth
good point!

Posted: Mon May 19, 2003 12:52 pm
by William_I
twigletmac wrote:
Skittlewidth wrote:Name validation is probably best done with Javascript
Always good to validate server side though in case JavaScript is turned off:
http://php.net/ctype
/quote]

But ctype_alnum won't return a TRUE if it's all legal alphanumeric, yet contains spaces.

Posted: Mon May 19, 2003 2:50 pm
by twigletmac
Which is where regular expressions would probably come into it. Or perhaps one of the other ctype functions could return the information needed.

Mac

Posted: Mon May 19, 2003 7:28 pm
by McGruff
Another fn you can use to check for the existence of a string in a string: substr_count().