I want to turn an array $badwords into a file to be called

147 Views Asked by At

I have a form that sends an email. I have a list of words to ban, and they are manually entered in an array. Each word in the array gets a point which eventually can reject a mail send. I want to put the words into a file instead to call up because though this works, its slow to update especially across several domains. Sorry for my lack of skill. Thanks.

$badwords = array("word1", "word2", "word3");
foreach ($badwords as $word)
  if (strpos(strtolower($_POST['comments']), $word) !== false

As the badwords add up, the point value increase to a limit which then rejects the send.

Excuse me, I was not clear evidently. I want to take the EXISTING array of badwords and put them in a file, in some sort of order and entry (line per line, or comma separated?). I want to call that file to be read by the existing script. So maybe it theoretically looks like : $badwords = badwords.php and so on.... Thanks

3

There are 3 best solutions below

0
On

In words.php:

<?php

   $words = ["filter","these","words","out"];

In your main script:

<?php

   include "words.php";
   print_r($words);

Result:

Array
(
    [0] => filter
    [1] => these
    [2] => words
    [3] => out
)
0
On

I'm not sure if that's what you need? Try it. This code should solve what you need. Find 'badwords' from the 'bedwords' list in the 'message', calculate the occurrence of word each of the 'bedwords' and add 1 penalty point to the '$ penalty' for each positive result (even duplicate). the code ignores uppercase and lowercase letters. Set the list:

$badwords = ['world', 'car', 'cat', 'train',];
$message = "World is small. I love music and my car. But I also love to 
            travel by train. I like animals, especially my cat.";

We will initialize the variable for counting penalty points.

$penalty = 0;

Now we need to go through the 'message' as many as there are in the 'badwords' fields. We will use the 'for' loop.

for($k =0; $k <= count($badwords) - 1; $k++):
preg_match_all("/$badwords[$k]/i", $message, $out[])
endfor;

We have now passed a total of 5 (from 0 to 4) through the message loop. Using a regular expression, we store word matches in an 'out' array, creating a multidimensional array. Now you need to go through this "out" field. We reduce its dimensions.

foreach ($out as &$value): 
      $value = $value[0];
  endforeach;

We will now go through this out field again using the 'for' loop and calculate the number of values in each dimension. Based on the calculated values we will assign 1 penalty point for each match and a duplicate.

  for($n = 0; $n <= count($out)-1; $n++):
  $penalty += count($out[$n]);
 endfor;

The result is the number of points awarded.

Here is source of the php, on PHP Fiddle http://phpfiddle.org/main/code/jzyw-hva6

0
On

figured it out.

in the root of my webspace I made a file called words.php

<?php
$badwords = array("000", "adult", etc

then added an include (as there are counted words, so can be more than one) to my main file

include "../badwords.php";  // the array list was here

and on to the foreach statement.

and removed this original line from that main file.

$badwords = array("word1", "word2", "word3");

Seems to be working. Thanks