Yes, it is possible and there are several approaches you could follow.
As it was already suggested, you could try to explode the chunk of text into an array, each element containing a single word, but I advise against it as your array would be very large if you had a very long chunk of text.
I would use regular expressions, which are a very powerful tool and would make your life much easier. To give you an idea of what you could do, the following snippet of code looks for the keywords contained in the array into the first paragraph of "Lorem Ipsum":
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc quam dui, mattis consequat posuere non, egestas sit amet ante. Vivamus vel ante vitae ipsum fringilla malesuada quis vitae est. Nullam condimentum augue at mauris congue ac scelerisque justo posuere. Curabitur sed dui id sem dignissim tempor. Ut dolor libero, volutpat nec ornare nec, tempor non sem. Phasellus a tortor in diam volutpat euismod. Morbi lacinia pharetra libero, sed aliquam urna volutpat sit amet. Fusce felis augue, dictum sit amet tincidunt at, venenatis eget nunc. Praesent auctor lacinia dolor, sit amet viverra tortor suscipit ac. In hac habitasse platea dictumst. Donec mi magna, sodales blandit condimentum sit amet, iaculis tristique nisi.";
$keywords = array('Google', 'adipiscing', 'viva', 'curabitur', 'dictumst');
foreach($keywords as $keyword)
{
preg_match_all("/\b".$keyword."\b/i", $text, $matches);
echo "Matches for $keyword:
";
print_r($matches);
echo "
";
}
If you run the code exactly as I listed it, you will get the following output:
Matches for Google:
Array ( [0] => Array ( ) )
Matches for adipiscing:
Array ( [0] => Array ( [0] => adipiscing ) )
Matches for viva:
Array ( [0] => Array ( ) )
Matches for curabitur:
Array ( [0] => Array ( [0] => Curabitur ) )
Matches for dictumst:
Array ( [0] => Array ( [0] => dictumst ) )
The essential part of this code is this line:
preg_match_all("/\b".$keyword."\b/i", $text, $matches);
If you have never used regular expressions before, you will probably want to do some reading on the subject. What that line does, basically, is to look in $text for the pattern defined by "/\b".$keyword."\b/i". The / is one of the possible delimiters that are commonly used to define a pattern, whereas the i following the last / means that the search should be case insensitive. The \b before and after $keyword stands for "word boundary", which means that only a complete match should be found, regardless or whether there is any punctuation immediately following or preceding a keyword. In light of that, the output of my example can be explained as this:
- keyword "Google": no matches, since the word doesn't appear in the text
- keyword "adipiscing": one match
- keyword "viva": no matches, even if the text contains "vivatur" (if \b were removed, you'd obtain one match)
- keyword "curabitur": one match, even if it is spelled with an uppercase "C" in the text
- keyword "dictumst": one match, even if in the text it is immediately followed by a "." and thus appears as "dictumst."
If you decided to follow the approach of splitting the original text in an array of single words, please note that the syntax used in the first answer is incorrect.
You could use something like this:
$words = str_word_count($text, 2);
str_word_count() is a PHP function that would pretty much do the job, providing you an array where the key is the the position of a word in the original text, and the value would be the word itself.
Alternatively, you could also use something like this:
$words = preg_split("/[\s,;:.]+/", $text);
This would split a chunk of text into words, using space characters and punctuation marks as delimiters.