1
public static function likecheck($str, $searchTerm) {
    $searchTerm = strtolower($searchTerm);
    $str = strtolower($str);
    $pos = strpos($str, $searchTerm);
    if ($pos === false)
        return false;
    else
        return true;
}

it works fine to match any single word from $str

$found = App\Helper::likecheck('Developers are very good','very');
if($found){
    echo 'ok';
}

But I want to check by giving more words separated by commas, if any of the words is found then return true

LIKE:

$found = App\Helper::likecheck('Developers are very good','very, good, gentle');
if($found){
    echo 'found';
}

But it does not as it can check only one word.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Anonymous Girl
  • 582
  • 8
  • 22

1 Answers1

2

I will recommend you to pass an array instead of single or comma separated string.

As well as recommend to use explode() and array_intersect()

public static function likecheck($str, $searchTerm =array()) {
    
    $explodedString = explode(' ', strtolower($str));
    $searchTerm = array_map('strtolower', $searchTerm);
    
    if(count(array_intersect($explodedString,$searchTerm)) > 0){
        return true;
    }else{
        return false;
    }
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98