Jump to content

[SOLVED] regex newbie question


mdnghtblue

Recommended Posts

I don't know much about regular expressions, and I'm trying to figure something out. I didn't write the following code:

 

$altbegin[] = "/([^\^\.\+\[\] ])/i";
$altend[] = "(\\\*|#|\\\^|\\1)([\.\_\~\`\+\=\* \-]*)";
$altbegin[] = "/uc/i";
$altend[] =   "(uc|u|cu|\*|\*c|u\*)";
$altbegin[] = "/a/i";
$altend[] =   "[a@ÁÂÃÄÅàáâãäå]";
$altbegin[] = "/b/i";
$altend[] =   "[bß]";
$altbegin[] = "/c/i";
$altend[] =   "[c©Çç\(\{\[]";
$altbegin[] = "/d/i";
$altend[] =   "(d|Ð|ð|\|\))";
$altbegin[] = "/e/i";
$altend[] =   "[eÆÈÉÊËèéêë3]";
$altbegin[] = "/f/i";
$altend[] =   "[fƒ]";
$altbegin[] = "/g/i";
$altend[] =   "[g9]";
$altbegin[] = "/h/i";
$altend[] =   "(\|-\||[h])";
$altbegin[] = "/l/i";
$altend[] =   "(£|i)";
$altbegin[] = "/i/i";
$altend[] =   "[il|\\\/!ÌÍÎÏìíîï]";
$altbegin[] = "/j/i";
$altend[] =   "(_\||[j])";
$altbegin[] = "/k/i";
$altend[] =   "(\|\(|\|\(|[k])";
$altbegin[] = "/m/i";
$altend[] =   "n";
$altbegin[] = "/n/i";
$altend[] =   "(\|\\\\\\\\\||[mnÑñ])";
$altbegin[] = "/o/i";
$altend[] =   "u";
$altbegin[] = "/u/i";
$altend[] =   "[ou0ÒÓÔÕÖòóôõöÙÚÛÜØøùúûüµ]";
$altbegin[] = "/r/i";
$altend[] =   "[r®]";
$altbegin[] = "/s/i";
$altend[] =   "[s\$§]";
$altbegin[] = "/y/i";
$altend[] =   "[yÿýÝ]";
$altbegin[] = "/^\(\\\\\\*\|#\|\\\\\\^\|/i";
$altend[] =   "(";

$swear_words = Array([insert swear words here]);

function swearCheck(&$str) {
global $config, $altbegin, $altend, $swear_words;
$str = ereg_replace("\"",""",$str);
$str = strip_tags($str,"<em><b><i><u><font><strong><h3><p><li><ul>");
$str2 = "- ".stripslashes(strip_tags($str))." -";
$fs = "(".preg_replace($altbegin,$altend,"fucker").")*(".preg_replace($altbegin,$altend,"sucker").")*";
for ($i = 0; $i < count($swear_words); $i++) {
	$word = preg_replace($altbegin,$altend,$swear_words[$i]);
	if (eregi("(.+[^A-z])($word)$fs([^A-z].+)",$str2,$vars)) {
		TheEnd("Profanity (even words like '$swear_words[$i]') is not allowed in $config[servname]!<BR><b>Profanity found in the following context:</b><p>...".substr($vars[1],-100)."<b>$vars[2]</b>".substr($vars[count($vars)-1],0,100)."...");
	}
}
}

 

I know it's the swear filter, but I'm not really sure how it works. (and not sure what exactly is contained in the altbegin and altend arrays. The previous maintainer of this code added "com" to the filter (to prevent spamming), and now I want to take it out, but I didn't see in the $swear_words array. How do I remove it from the filter?

Link to comment
Share on other sites

Adding some test code should help:

 

### Create a list of words that will be considered swear words.
$swear_words = Array(
	'Damn',
	'Poop',
);

### Create a list of words to test against the filter.
$words = array('Dam', 'Damn it!', 'Egad!', 'Poop...');
foreach ($words as $word) {
	echo "Checking <b>$word</b>...<br>";
	swearCheck($word);
}

### Add the code you posted.

 

You define what the swear words are and they are transformed into expressions (you can see these by echoing out $word after it is defined). The $altbegin array is the "search for" array, and the $altend array is the "replace with" array. They are paired together. For example:

 

$altbegin[] = "/uc/i";
$altend[] =   "(uc|u|cu|\*|\*c|u\*)";

 

If your swear word contains "uc" it will be transformed into the pattern "(uc|u|cu|\*|\*c|u\*)" which matches "uc", "u", "cu", "*", "*c", or "u*".

 

The function is not efficient because it recreates the patterns on every call and for every word checked.

 

I don't see the "com" you're referring to. Is it in another piece of code?

Link to comment
Share on other sites

I'll try putting in some test code in to see how it works.

 

The code that stops "com" from being used has got to be in the code I posted above. I couldn't find it in the swearwords array, so I thought it was hidden in one of the regular expressions.

 

The filter thinks "com" is "cum", so when someone tries to use an email address or URL with "com" in it, they'll get the following error message:

 

Profanity (even words like 'cum') is not allowed in the game!

Link to comment
Share on other sites

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.