match - matching words from a file exactly in php -


im trying see if words in string match words in file exactly. had sentance:

"i love football"

foreach ($match $macthes)                 {                     if (stripos("i love football",$match) !== false) {                         break;                     }                 }                    here 

the above method works, find love , want too, works when types shorter version of word, searching "pleased" put in "please" show result , dont want that.

is there way can run method if exact match found?

to find complete word can use regular expression word boundary character: \b

if (preg_match('/\b'.preg_quote($emotion).'\b/', $value['message'])) { 

the important part put \b both before , after word or phrase looking for, denotes word boundary:

a word boundary position in subject string current character , previous character not both match \w or \w (i.e. 1 matches \w , other matches \w), or start or end of string if first or last character matches \w, respectively. http://uk3.php.net/manual/en/regexp.reference.escape.php

or more how we're using it: characters before & after string match must doesn't continue word. such beginning or end of file/line, or whitespace, punctuation, etc.

we're using preg_quote, make sure if of $emotion values include special characters escaped don't alter meaning of expression.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -