Reputation: 627
I'm building a little bot in PHP and I'm wondering how to use preg_match() to return 'Hello There'; to all inputs of words like: hey, hello, hi, sup, herro, hiii. From what I vaguely recall its
if(preg_match('[hi¦hey¦hii¦hello¦sup]', $string)){
echo 'Hello There';
}
Where ¦ = the straight line (I'm on mobile, don't have that key :(... Nor can I code format)
Also, is it possible to do it with checking alternate phrases?
I guess switch case break could do it, but its a bot that is going to have around 40 functions, and I guess that's going to be quite a mess.
Thanks a lot :)
Upvotes: 0
Views: 90
Reputation: 3006
Following on from @jogesh_p it might be worth looking at the levenshtein function. As it's not possible to account for every single greeting combination. The levenschtein function calculates the distance a string is from another one - it's how search engines say things like "did you mean: ...".
Upvotes: 1
Reputation: 9782
as i understand hope this will help you:
$str = "hi this is my string";
$parrt = '/(hi|hey|hii|hello|sup)/i';
if( preg_match( $parrt, $str ) ){
echo "Hello There!";
}
else{
echo "Not Match!";
}
Upvotes: 2