Page 1 of 3
Badword Seach in String or Variable
Posted: Fri Jun 02, 2006 8:47 am
by tecktalkcm0391
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."
Posted: Fri Jun 02, 2006 8:58 am
by Roja
Take a look at the
strstr() function.
Posted: Fri Jun 02, 2006 2:28 pm
by tecktalkcm0391
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;
}
}
?>
Posted: Sat Jun 03, 2006 12:31 pm
by tecktalkcm0391
This is still not working. Can anybody help. It wont find stuff like 'badword' in the username 'iliketousebadwords'
Posted: Fri Jun 09, 2006 8:56 pm
by tecktalkcm0391
I can't figure this out:
I keep getting the message:
Notice: Array to string conversion in /home/public_html/user.php on line 123
And were I the $bword[] is it just comes up with array, or if i use print_r() it comes up with:
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;
}
}
Any help?
Posted: Sat Jun 10, 2006 10:12 am
by aerodromoi
Here you go:
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";
}
?>
aerodromoi
Posted: Sat Jun 10, 2006 12:13 pm
by Li0rE
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.
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.");
}
?>
just make sure you write all the bad words lowercase.
Posted: Sun Jun 11, 2006 11:00 am
by tecktalkcm0391
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);
?>
Posted: Sun Jun 11, 2006 11:49 am
by aerodromoi
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);
?>
This two-dimensional array would enable you to replace a specific badword with a specific string.
However, as you're only trying to replace all badwords with *, preg_replace should be sufficient.
aerodromoi
Posted: Sun Jun 11, 2006 3:44 pm
by tecktalkcm0391
aerodromoi wrote: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);
?>
This two-dimensional array would enable you to replace a specific badword with a specific string.
However, as you're only trying to replace all badwords with *, preg_replace should be sufficient.
aerodromoi
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?
Posted: Sun Jun 11, 2006 4:10 pm
by aerodromoi
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?
Why so complicated?
Here's a hint: $badwords[$i][0]... (and you won't need array_change_key_case)
aerodromoi
Posted: Sun Jun 11, 2006 4:22 pm
by tecktalkcm0391
Posted: Sun Jun 11, 2006 5:38 pm
by tecktalkcm0391
aerodromoi, I keep getting a
Notice: Array to string conversion in /home/public_html/Login/functions.php on line 320
I have it set up so on functions.php it has:
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;
}
?>
On Badword.php i have:
Code: Select all
<?php
$badwords = array();
$badwords[] = array(' bad ', ' * ');
$badwords[] = array(' \@\$\$ ', ' * ');
$badwords[] = array(' a\$\$ ', ' * ');
$badwords[] = array(' words ', ' * ');
?>
And on the main page I have:
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";
}
?>
What could be wrong? It keeps giving me the
Notice: Array to string conversion in /home/public_html/Login/functions.php on line 320
For every item in the array, which is 271 items, or badwords.
Posted: Sun Jun 11, 2006 7:13 pm
by John Cartwright
... 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.
Posted: Sun Jun 11, 2006 9:37 pm
by tecktalkcm0391
yeah, i'll use
Code: Select all
array_change_key_case($badwords[$i],CASE_LOWER);
if that is right... is it?