Help with PHP code - Multiple Variables?

10-04-09, 12:32 PM
|
|
Newbie Coder
|
|
Join Date: Oct 2009
Posts: 61
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
Help with PHP code - Multiple Variables?
Hello
This is part of my PHP code:
$badwords = "rubbish";
and another part
if($username == $badwords){
this PHP code works when you type rubbish in the username, but I want to add extra words into $badwords, but I can't, please tell me how.
if you can do this, thank you so much! thank you for saving so much time for me!

ultimatewarrior
|

10-04-09, 12:58 PM
|
 |
Coding Addict
|
|
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
|
|
You need to use an array. Then loop through them:
*This code only prints the bad words, I would have included your if statement in the code but you didnt give it all.
**This code has the bad words hard coded, if I were you I would have them saved in a DB or in a flat file even for easy updating.
__________________
This post was created with 100% recycled electrons.
Last edited by ruteckycs; 10-04-09 at 01:01 PM.
|

10-04-09, 05:06 PM
|
 |
-
|
|
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
|
|
|
|

10-04-09, 05:59 PM
|
 |
Coding Addict
|
|
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
|
|
First time I have heard about in_array() this is a good example of me doing things the 'hard way'. Question about it though. Is the same running time as a bunch of ifs or is this faster?
__________________
This post was created with 100% recycled electrons.
|

10-04-09, 06:19 PM
|
 |
Community Leader
|
|
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
|
|
It's much faster.
Make sure to trim() the username as well.
|

10-04-09, 06:51 PM
|
|
Newbie Coder
|
|
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Is it recommended to use in_array() even if the list is somewhat lengthy, as in over 20-30 items?
I don't intend on having a list that long, because strlower() eliminates the need to check multiple variations based on capitalization, but I'm just curious.
Also, is it possible to check the input for things such as underscores or dashes between letters of "banned" words? (i.e.
user-name, user_name, u_sern_ame etc.) or would you recommend letting people use them?
|

10-04-09, 07:28 PM
|
 |
Coding Addict
|
|
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
|
|
20-30 is not a long list. When I said a long list in my post to wirehoper I was thinking 15,000 or so.
You can use remove any underscores
You can make it to remove multiple things (underscore , dashes , dots, commas)
__________________
This post was created with 100% recycled electrons.
|
|
The Following User Says Thank You to ruteckycs For This Useful Post:
|
|

10-04-09, 07:38 PM
|
|
Newbie Coder
|
|
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Thanks ruteckycs. I wasn't sure what was considered a "long" list and accidentally left off a zero in my post.
But if you consider something like 15,000 a long list then I won't have to worry about it for my purposes. Thanks for the help on removing characters from input via preg_replace.
|

10-04-09, 11:03 PM
|
 |
-
|
|
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
|
|
Nico - good point about trim.
If you find you have a really long list, I'd store it in a text file and use grep, on the commandline. It's unlikey you'll ever need it.
|

10-05-09, 11:33 AM
|
|
Newbie Coder
|
|
Join Date: Oct 2009
Posts: 61
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
Thanks! I will try them out! =D
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|