Syed
Syed

Reputation: 931

find bad word using strpos from string

I have following php code to find bad word in a string.

It stop on first bad word found and return true.

The bad words are provided as comma separated list that is converted to array.

$paragraph = "We have fur to sell";
$badWords = "sis, fur";
$badWordsArray = explode(",", $badWords);

function strpos_arr($string, $array, $offset=0) { // Find array values in string
    foreach($array as $query) {
        if(strpos($string, $query, $offset) !== false) return true; // stop on first true result for efficiency
    }
    return false;
}

strpos_arr($paragraph, $badWordsArray);

The issue is it also returns true if bad word provided is a part of another word.

I prefer using strpos. Please also suggest if there is any more efficient way to find bad words.

Upvotes: 2

Views: 334

Answers (2)

spirit
spirit

Reputation: 3415

try this, with reqular expression:

$paragraph = "We have fur to sell";
$badWords = "sis, fur";
$badWordsArray = preg_split('/\s*,\s*/', $badWords, -1, PREG_SPLIT_NO_EMPTY);
var_dump($badWordsArray);

function searchBadWords($string, $array, $offset=0) { // Find array values in string
    foreach ($array as $query) {
        if (preg_match('/\b' . preg_quote($query, '/') . '\b/i', $string)) return true; // stop on first true result for efficiency
    }
    return false;
}

var_dump(searchBadWords($paragraph, $badWordsArray));

Explanation:

First. We want to correctly split our $badWords string:

$badWordsArray = preg_split('/\s*,\s*/', $badWords, -1, PREG_SPLIT_NO_EMPTY);

This way we will correctly split strings like "sis, fur" and "sis , fur" and even "sis , , fur" to an array('sis', 'fur').

Then we are performing regexp-search of exact word using \b meta-character. Which means word-boundary in terms of regular expression, that is position between a word-characted and a non-word-character.

Upvotes: 2

candidev
candidev

Reputation: 38

Just include spaces in your search string.

$paragraph = "We have fur to sell";
$badWords = "sis, fur";
$badWordsArray = explode(",", $badWords);

function strpos_arr($string, $array, $offset=0) { // Find array values in string

$string = " ".$string." ";

foreach($array as $query) {
    $query = " ".$query." ";
    if(strpos($string, $query, $offset) !== false) return true; // stop on first true result for efficiency
}
return false;
}

strpos_arr($paragraph, $badWordsArray);

Upvotes: 0

Related Questions