Badword Seach in String or Variable
Moderator: General Moderators
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
Badword Seach in String or Variable
How do I have PHP pick out the specific words I tell it to in an array, and it finds it in the variable. I mean like if I want to block bad, and the user submits 284bad3845 it would pick it out, and then return a message "Sorry, your username has the word bad in it, you can't have that work in your submition, please try again."
Last edited by tecktalkcm0391 on Wed Jun 21, 2006 8:38 pm, edited 2 times in total.
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
I am using the strstr() funciton. See:
Code: Select all
<?php
$username_bad_check = $username;
$username_bad_check = strtolower($username_bad_check);
$badwords = array();
$badwords[] = array('ass', ' * ');
foreach ($badwords as $bword)
{
if (strstr($username_bad_check, $bword)) {
echo "ERROR";
exit;
}
}
?>- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
I can't figure this out:
I keep getting the message:
Also if the bad word is word, I get bad instead of word.
Any help?
I keep getting the message:
And were I the $bword[] is it just comes up with array, or if i use print_r() it comes up with:Notice: Array to string conversion in /home/public_html/user.php on line 123
Array ( [0] => ass [1] => * )
Also if the bad word is word, I get bad instead of word.
Code: Select all
$username_bad_check = $username;
$username_bad_check = strtolower($username_bad_check);
$badwords = array();
$badwords[] = array('bad', ' * ');
$badwords[] = array('word', ' * ');
array_change_key_case($badwords,CASE_LOWER);
/*$username_bad_check = implode("\r\n", $username_bad_check); */
foreach ($badwords as $bword)
{
if (str_replace($username_bad_check, $badwords, $bword)){
echo "OH NO YOU DID'T. We have found a bad word in your username! You may have put something bad in by mistake, but remember if you use bad words* on our site.<br><b>Your username can not contain the word: </b>" . $bword[] . "<br><br><br><Br>*The term \"Bad Word\" refeers to a curse words, an explicit words, or any other inappropriate word, or word we find to be inappropriate." ;
exit;
}
}- aerodromoi
- Forum Contributor
- Posts: 230
- Joined: Sun May 07, 2006 5:21 am
Here you go:
aerodromoi
Code: Select all
<?php
function findbwords($string,$bwarray){
$found = false;
for($i=0;$i<count($bwarray);$i++){
if(strstr(strtolower($string), strtolower($bwarray[$i]))) $found = $bwarray[$i];
}
return $found;
}
$badw_array = array("donkey","moron","xxx");
$username = "usemoronrnx xxam e";
if (findbwords($username,$badw_array)){
echo "Error - Your username contains the word: ".findbwords($username,$badw_array);
}
else{
echo "username accepted";
}
?>theres another thing you can do that might take less time than the for loop. I just thought of this, dont know if its a good idea.
just make sure you write all the bad words lowercase.
Code: Select all
<?php
$badwords = array(poopie, doodie, peepee, apple); //excuse my french!
$delete = array("");
$username = $_POST['username']; //Get the username variable from form
$tester = strtolower($username); //Convert all to lowercase
$tester = str_replace($badwords, $delete, $tester); //Make a tester with bad words deleted
if(strlen($username)==strlen($tester)) //If the tester hasnt been effected, no bad words are present.
{
echo("Your username is ok! It contains no bad words.");
}
else //If anything in tester has been altered, it means there was a bad word present, so do this:
{
echo("Try again. Your username contained a bad word.");
}
?>- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
Thanks, but what can I do with the array that i already have in this format:
Code: Select all
<?php
$badwords = array();
$badwords[] = array('bad', ' * ');
$badwords[] = array('word', ' * ');
array_change_key_case($badwords,CASE_LOWER);
?>- aerodromoi
- Forum Contributor
- Posts: 230
- Joined: Sun May 07, 2006 5:21 am
This two-dimensional array would enable you to replace a specific badword with a specific string.tecktalkcm0391 wrote:Thanks, but what can I do with the array that i already have in this format:
Code: Select all
<?php $badwords = array(); $badwords[] = array('bad', ' * '); $badwords[] = array('word', ' * '); array_change_key_case($badwords,CASE_LOWER); ?>
However, as you're only trying to replace all badwords with *, preg_replace should be sufficient.
aerodromoi
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
I know that, the array I have already has about 300 lines in it of that, and instead of messing with and changing it all, could I make the badword blocker like aerodromoi said, but with the array I currently have. If so, can I just have the array on another PHP Page, by itself, and then just include it before the blocker is activated?aerodromoi wrote:This two-dimensional array would enable you to replace a specific badword with a specific string.tecktalkcm0391 wrote:Thanks, but what can I do with the array that i already have in this format:
Code: Select all
<?php $badwords = array(); $badwords[] = array('bad', ' * '); $badwords[] = array('word', ' * '); array_change_key_case($badwords,CASE_LOWER); ?>
However, as you're only trying to replace all badwords with *, preg_replace should be sufficient.
aerodromoi
- aerodromoi
- Forum Contributor
- Posts: 230
- Joined: Sun May 07, 2006 5:21 am
Why so complicated?tecktalkcm0391 wrote: I know that, the array I have already has about 300 lines in it of that, and instead of messing with and changing it all, could I make the badword blocker like aerodromoi said, but with the array I currently have. If so, can I just have the array on another PHP Page, by itself, and then just include it before the blocker is activated?
Here's a hint: $badwords[$i][0]... (and you won't need array_change_key_case)
aerodromoi
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
aerodromoi, I keep getting a
On Badword.php i have:
And on the main page I have:
What could be wrong? It keeps giving me the
I have it set up so on functions.php it has:Notice: Array to string conversion in /home/public_html/Login/functions.php on line 320
Code: Select all
<?php
require_once("../Include Content/Badword.php"); //Location of Badword Array
function find_badwords($string, $badwords){
trim($badwords, " "); //Added this to fix spaces in the array -- more or less, take out the spaces that were already there
$found = false;
for($i=0;$i<count($badwords);$i++){
if(strstr(strtolower($string), strtolower($badwords[$i]))) $found = $badwords[$i][0];
}
return $found;
}
?>Code: Select all
<?php
$badwords = array();
$badwords[] = array(' bad ', ' * ');
$badwords[] = array(' \@\$\$ ', ' * ');
$badwords[] = array(' a\$\$ ', ' * ');
$badwords[] = array(' words ', ' * ');
?>Code: Select all
<?php
include("functions.php");
require_once("../Include Content/Badword.php");
$username = 'badusername';
if (find_badwords($username, $badwords)){
echo "Error - Your username contains the word: ".findbwords($username, $badwords);
}
else{
echo "username accepted";
}
?>For every item in the array, which is 271 items, or badwords.Notice: Array to string conversion in /home/public_html/Login/functions.php on line 320
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
... strtolower($badwords[$i]))) ...
I would assume that is causing the error, considering your applying an array to a string function. Secondly, you might want to reconsider your whole setup as the logic and maintainability of the code seems odd. We've recently had a thread about a bbcode class which you may find of interest.
I would assume that is causing the error, considering your applying an array to a string function. Secondly, you might want to reconsider your whole setup as the logic and maintainability of the code seems odd. We've recently had a thread about a bbcode class which you may find of interest.
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
yeah, i'll use if that is right... is it?
Code: Select all
array_change_key_case($badwords[$i],CASE_LOWER);