Searching a string for one word...

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
William_I
Forum Newbie
Posts: 13
Joined: Sat Mar 29, 2003 8:22 am
Location: Manhattan

Searching a string for one word...

Post 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.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post 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)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

good point!
William_I
Forum Newbie
Posts: 13
Joined: Sat Mar 29, 2003 8:22 am
Location: Manhattan

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Another fn you can use to check for the existence of a string in a string: substr_count().
Post Reply