From a list of words, we want to find what word occurrs first inside a given input string.
Solution
$arrWords = array("one", "two", "three"); $strInput = "Test a paragraph three and testing some more one test test two"; function getFirstOccurrence($strInput, $arrWords) { $arrInput = explode(" ", $strInput); foreach($arrInput as $strInput) { if(in_array($strInput, $arrWords)) { return $strInput; } } return null; } print "First word is: " . getFirstOccurrence($strInput, $arrWords);