Jump to content

Recommended Posts

I can't seem to find a decent code function to search for "curse" words that i would deem inappropriate in an array. Count the number of instances the function finds a curse word and then if the number is greater than one halt the script.

 

something like the following but not really....

$words = array('ass','fuck','shit');
$string = 'Blah blah blah ass blah blah fuck blah blah shit!';
//code to count the instances of the curse words(3)
if($instances > 0)
{
echo "no cursing please.";
} ;

 

any help on how to do this would be much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/
Share on other sites

try this mate, no offence to other poster, but I can't see that snippet working, or maybe I am just going mad:

<?php
$i = 0;
                $words = array('ass','fuck','shit');
$string = 'Blah blah blah ass blah blah fuck blah blah shit!';
foreach ($words as $value) 
{
                                 if (strpos($string,$value))
	{
		$i ++;		
                                 }
                 if($i>3) echo "naughty words limit";	
}
?>

 

this should do the trick, the mistake i made was with instr() it didnt count number of times a rude word might appear, so was looking for 3 different rude words, rather than a total of 3 rude words, be they all different or the same.

 

EDIT*** i should be shot for skim reading, this allows you to have 2 rude words and not more, really as soon as you get a hit of 1 curse word you want it to stop. i will post YET A?NOTHER solution haha i think i need to go to bed!! ******

 

<?php
$i = 0;
$limit = false;
$words = array('ass','fuck','shit');
$string = 'ass ass blah blah ass blah blah shit  blah ass!';
foreach ($words as $value) 	{
$i += substr_count($string,$value);// includes multiple counts of the same naughty word
if($i>=3)
	{
		$limit = true;
		echo "naughty word limit";
	}
if ($limit)break;// stops the search going any further
}	
?>

3rd time lucky (god i hope so!)

<?php

$words = array('ass','fuck','shit');
$string = 'Blah blah blah ass blah blah fuck blah blah shit!';	
foreach ($words as $value) 	
{	                                 
if (strpos($string,$value))
	{			
		echo "naughty words limit";
		break;
	}                 

}	
?>	

Didn't see post above. I recommend using his way, less code. ;)

 

Tested, and works. ;)

 

<?php

//bad words 0 1 2
$words = array('asshhfghh', 'gfhfghfgh', 'fuck');

//string to search
$string = 'Blah blah blah ass blah blah fuck blah blah shit!';

//amount in the array
$x = count($words) + 1;

while($i < $x)
{
	if(strpos($string, $words[$i]) === false)
	{
		++$i;
	}
	else
	{
		//holds the number of bad words found
		++$b;
		++$i;

	}
}

if(!$b)
{
	echo "No bad words found, yippe!";
}
else
{
	echo "Bad words found.";
}

?>	

yeah what confused me most was that I thought a count was necessary, when really its a sort of boolean search, there either is or isnt a curse word, one instance will make it true.

 

thats why my final snippet was much shorter, as no counting was involved

hmm..i tried to implement this code

 

<?php
$words = array( 'fuck','shit', 'dick', 'cunt', 'twat', 'motherfucker', 'asshole', 'damn', 'bitch', 'bitching', 'fucking', 'fuckin', 'shitting', 'shittin', 'whore', 'whores', 'slut', 'sluts', 'bullshit', 'ass', 'bitches');


//string to search

$string = $sentence;

//amount in the array

$x = count($words) + 1;

while($i < $x)
{
if(strpos($string, $words[$i]) === false)
{
++$i;
}
else
{
//holds the number of bad words found
++$b;
++$i;
}
}


if(!$b)
{
//echo "No bad words found, yippe!";
}
else
{
	$_SESSION['message'] = "Your sentence contains curse words. Please remove the foul language.";
	die(header("location: test.php"));
}

 

but it doesn't count them as individual words. For example "I want to go bass fishing" will throw an error for the word "bass" because "ass" is in it. Any ideas how to make it only search for words? It doesn't seem to work if i just put spaces around the words in the array.

<?php
$words = array( 'fuck','shit', 'dick', 'cunt', 'twat', 'motherfucker', 'asshole', 'damn', 'bitch', 'bitching', 'fucking', 'fuckin', 'shitting', 'shittin', 'whore', 'whores', 'slut', 'sluts', 'bullshit', 'ass', 'bitches');

//string to search

$string = $sentence;

//amount in the array

$x = count($words);

for($i = 0; $i < $x; ++$i)
{
if(strpos($string, $words[$i]) === false)
{
++$i;
}
else
{
//holds the number of bad words found
++$b;
++$i;
}
}


if(!$b)
{
//echo "No bad words found, yippe!";
}
else
{
	$_SESSION['message'] = "Your sentence contains curse words. Please remove the foul language.";
	die(header("location: test.php"));
}
?>

 

This will count them as individual words.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.