The Spam-Filter package defines an object-oriented layer for checking strings about spam.
The Regex Filter is build to match Spam by an array of Regex patterns. If a regex pattern result matches the input will rate as spam.
The Blacklist Filter is used to match Spam by an array of patterns. If the input contain a pattern it will rate as spam .
The Whitelist Filter is build to check if the input contains any whitelisted patterns. If no whitelisted pattern contained the input will rate as spam.
The URL Filter is build to match if the input contains url's.
The Duplication Filter is build to match word duplications in text's.
The $minConsecutiveWords
defines if only words or sentences with a specific length will be matched.
Warning: For larger text this filter performs a lot of operations.
$spamFilter = new \Flexic\SpamFilter\SpamFilter([
new \Flexic\SpamFilter\Filter\BlacklistFilter([
'want to buy'
]),
new \Flexic\SpamFilter\Filter\RegexFilter([
'/\[\[url\=.*\]\]/'
])
]);
$spamFilter->check(
'Hello, how are you? Do you want to buy [[url=https://example.com/product]] this Product?'
); // Result: true (both filters detected spam)
$spamFilter->check(
'Hey my friend. Do you want to test a new PHP Library?'
); // Result: false (No filter detected spam)
The Spam-Filter supports the creation of custom filters.
The Custom-Filters must implement the \ThemePoint\SpamFilter\Filter\FilterInterface
with it's defined functions.
Example:
class EmailFilter implements \Flexic\SpamFilter\Filter\FilterInterface
{
public function check($input): bool
{
\preg_match_all('/(([^ ].*)@(.*))/', $input, $matches, \PREG_SET_ORDER, 0);
if (\count($matches) > 0) {
return true;
}
return false;
}
}
Pay attention to the following behavior of the check
function:
- Return
true
if spam is detected in input. - Return
false
if no spam is detected in input.
You can control how the Spam Filter react if an exception is thrown.
The value of $responseOnException
is equivalent to the filter results.
If you want that the Spam Filter defines an input as Spam if an exception is thrown set this value to true
.
[1.0]
- Initial commit of package