PHP preg_replace返回不同​​的值

I have two statement.

1) I like Car. 2) I wear cloths like king.

In both statement the meaning of like word is different. So I want to write function, If I passed first statement it should return 'a' & I passed second statement then it should return 'b'.

    function chkStatement($stmt) {
      //function body
    }
    chkStatement('I like an apple');
chkStatement('I like Mango');
chkStatement('I fly in air like a bird');

Thanks

Simple PHP functions like preg_replace won't allow you to return different values based on the context in which the word is used.

You will want to look into the vast universe of natural language processing. Good luck!

Marc-Antoine is right, the only way to identify like as a verb is with NLP.

However, if we simplify this enough, you could check for I, You, We, or They, before the word like. You wouldn't be checking if it's in fact a verb, and there should be lots of exceptions. You could use something like this:

function chkStatement($stmt) {
    if (preg_match('/\b(?:(I|You|We|They)\s+)?like\b/i', $stmt, $matches)) {
        if (isset($matches[1])) return "a";
        return "b";
    } else {
        return false;
    }
}

echo chkStatement('I like an apple');
echo chkStatement('I like Mango');
echo chkStatement('I fly in air like a bird');
echo chkStatement('Something else');