Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.5 - Check if string contains exact words

In laravel, I have a $string and a $blacklistArray

$string = 'Cassandra is a clean word so it should pass the check';
$blacklistArray = ['ass','ball sack'];

$contains = str_contains($string, $blacklistArray); // true, contains bad word

The result of $contains is true, so this will be flagged as containing a black list word (which is not correct). This is because the name below partially contains ass

Cassandra

However, this is a partial match and Cassandra is not a bad word, so it should not be flagged. Only if a word in the string is an exact match, should it be flagged.

Any idea how to accomplish this?

like image 633
Wonka Avatar asked Nov 12 '17 23:11

Wonka


People also ask

How do I check if a string contains a specific word in laravel?

Laravel has a Str helper function. Str class has contains() method that will provide to check string contains in laravel application. contains() take a two argument one should be string and another will be string or array.

How do I check if a string contains a specific word?

You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false . Also note that string positions start at 0, and not 1.

How do I check if a string contains something?

The includes() method returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.

What is :: In laravel?

Basically its know as Scope resolution operator (::) Simply it is token which allow access to static, constant and overridden properties of method of a class. Example- in laravel we call model like this.


4 Answers

Docs: https://laravel.com/docs/5.5/helpers#method-str-contains

The str_contains function determines if the given string contains the given value:

$contains = str_contains('This is my name', 'my');

You may also pass an array of values to determine if the given string contains any of the values:

$contains = str_contains('This is my name', ['my', 'foo']);
like image 190
set0x Avatar answered Oct 03 '22 15:10

set0x


$blacklistArray = array('ass','ball sack');

$string = 'Cassandra is a clean word so it should pass the check';



$matches = array();
$matchFound = preg_match_all(
                "/\b(" . implode($blacklistArray,"|") . ")\b/i", 
                $string, 
                $matches
              );

// if it find matches bad words

if ($matchFound) {
  $words = array_unique($matches[0]);
  foreach($words as $word) {

    //show bad words found
    dd($word);
  }

}
like image 45
Diego Cespedes Avatar answered Oct 03 '22 16:10

Diego Cespedes


Laravel Str::contains() method can check values against a work. Don't forget to use Illuminate\Support\Str;

Works on Laravel 7.x

Also accepts arrays of value. Instance:

$string = 'Cassandra is a clean word so it should pass the check'; $blacklistArray = ['ass','ball sack'];

if(Str::contains($string, $blacklistArray)){return true;}

// returns true

May not be your exact request though

like image 34
DAVID AJAYI Avatar answered Oct 03 '22 16:10

DAVID AJAYI


str_contains() works with strings - not with arrays, but you can loop it:

$string = 'Cassandra is a clean word so it should pass the check';
$blacklistArray = ['ass','ball sack'];

$flag = false;
foreach ($blacklistArray as $k => $v) {
    if str_contains($string, $v) {
        $flag = true;
        break;
    }
}

if ($flag == true) {
    // someone was nasty
}
like image 39
Tpojka Avatar answered Oct 03 '22 15:10

Tpojka