Badword Seach in String or Variable

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

User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Badword Seach in String or Variable

Post 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."
Last edited by tecktalkcm0391 on Wed Jun 21, 2006 8:38 pm, edited 2 times in total.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Take a look at the strstr() function.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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;
    }
}




?>
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

This is still not working. Can anybody help. It wont find stuff like 'badword' in the username 'iliketousebadwords'
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post 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
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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);


?>
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post 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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post 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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Thanks, I totally didn't think about that. :D :D :D :D :D Maybe when I start making money, I'll give you some! :D
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

yeah, i'll use

Code: Select all

array_change_key_case($badwords[$i],CASE_LOWER);
if that is right... is it?
Post Reply