douyou8047 2018-12-18 19:31
浏览 11

如何将字符串与文本组与PHP进行比较

I have a string group like ("hello", "hi", "how r u", "how are you", "how r you", "how are u", etc.) and i want to create a function that can compare a string varible (like $varible = "Helloooo") with string group. Regex or any method is useful to me.

String can be more but can not be missing for example:

Hello = should be true

Helloooo = should be true

How r youuu !!!! = should be true

hell = should be false

w are y = should be false

heey hello = should be true

heeeey hello bro = should be true

I'm talking about this string group ("hello", "hi", "how r u", "how are you", "how r you", "how are u", etc.)

I say "string group" because type doesn't have to be an array but it also may be an array. As i said any method is useful to me.

Thanks for you support.

  • 写回答

4条回答 默认 最新

  • duandanxiu6965 2018-12-18 19:47
    关注

    Here's a way to do it. As far as I can see you want it to be case-insensitive that's what the strtolowers are for

    $parts = ['hello', 'hi', 'how r u', 'how are you', 'how r you', 'how are u'];
    
    function wordContainsPart($parts, $word) {
            $word = strtolower($word);
            foreach($parts as $part){
                    $part = strtolower($part);
                    if(strpos($word, $part) !== false) {
                            return true;
                    }
            }
    
            return false;
    }
    
    评论

报告相同问题?