Is there a Php function to search for a list of words in a given text block?


What is the Php function to search for a list of words in a text and if one of the words matches then return true.

So say we want to search for these words:
$words = array(‘wag’, ‘dog’, ‘tail’);

for ($i=0, $i<count(documents), ++$i) {

$documnt = documents[$i];

if (find_words($words, $document)) {

 $match[$i] = 'yes';



so is there a Php function that would do what find_words() is doing
in above sample code?


Might work if you bothered to learn how to use preg_match.
I’m not going to hand you the answer on a silver platter.

You’ve been here long enough, for once try and solve your problems by learning.


I did what you suggested, but I am getting Error messages.

That is I have:

$fraud_words = array(‘dog’, ‘wag’, ‘tail’);


for ($i = 0; $i < count($fraud_words); ++$i) {

		$word = $fraud_words[$i];

		if (preg_match($word, $headline)) {

			$fraud = 'yes';


And I get this Error message:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in /var/www/html/ on line 368


Why are you bothering answering my question if your attitude is to answer a follow up question like this:
“I’m not going to hand you the answer on a silver platter.”

PLEASE do not answer any more questions of mine so that my time is not wasted reading
such crap from you.

I mean I gladly answer peoples question and provide all sort of volunteer work without insulting
whom I am helping. So please if you cannot do the same then do not bother to provide peace
meal help followed by an insult such as: “for once try and solve your problems by learning”

Thank you.

Have fun trying to solve it on your own then.

There is no built-in function for this. Try the following code

$words = array('wag', 'dog', 'tail');
$wordsPattern = '#(' . implode("|", $words) . ')#i';
foreach($documents as $doc)
        $found = preg_match($wordsPattern, $doc);

Bah, wasn’t suppose to give away the answer! D:<

gvre , ThanX I will try that.

gvre, I tried that with the method, but it is not working.
Can you suggest what to change to this code to get it to work OK?

for ($i = 0; $i < count($fraud_words); ++$i) {

		$word = $fraud_words[$i];
		$word = '/' . $word . '/i';

		if (preg_match($word, $headline)) {

			$fraud = 'yes';

		}    else {

			$fraud = 'no';


	}//CLOES For Loop

Oh boy, I just knew you would come back! Even when the answer is given to you on a silver platter…Seriously, one of these days you just need to stop and actually learn the fundamentals of PHP or any programming language before you come here begging for help with every tiny problem you run into. You may think I’m being cruel and mean with my attitude, well I am. No one learns anything by getting all the answer in life handed to them, why should programming be the same?

If you had any grasp of the fundamentals of PHP, or programming in general you could easily see the error you made. But you cannot because all you have ever done is ask for an answer and took it without question or understanding it. Do you want to know why your code does not work? Look real hard at it, imagine the process it goes though in your head. What is the result?

Furthermore, get ride of the for loop, do not use a foreach loop or any loop for that matter, its not required. gvre, gave you every single thing you needed, minus the foreach loop. How could you miss that?

To say that you are a rude obnoxious jerk is putting it mildly.
After all, 1st I asked you not to answer to my messages any further since I have no need of your attitude.

2nd, you have NO idea of what work load I have, given that I am literally doing 10 peoples work, at least.

3rd, and this is my last message to you EVER, for this new site that I am creating I have written about 8,000 lines
of Php code and have asked a total of 6 question on Sitepoint about it, one of which is/was this question.

Good day.

How about a loop using stripos?

And please, let’s keep it friendly.

Trying, but it is hard when they don’t bother to learn, instead want everything handed to them. Even when it is…bah.

I mean how hard is:

$words = join( array( 'word1', 'word2' ), '|' );
if ( preg_match( "/$words/i", $soruce ) )
   // found whatever in source

No stripos will not do the job.
Since I need to look for multiple words within a document.
But ThanX anyway.

Actually I think I have finally come up with a solution that does th ejob.

Congratulations on copy and pasting!

logic_earth, you need to get a life rather hanging around here like a troll and
insulting people rather than being of help.
I mean how many times do I have to ask you not to comment to my posts and
you still like a troll send 1 message after another! Really amazing :frowning:

At the risk of getting smacked for backseat moderation - knock it off, both of you. If you can’t stand each other’s posts do what I do with the people here I can’t stand and use the ignore poster feature, aka the “killfile”

Why not take each word at a time and see if it exists in the document with strpos()?

function find_words($words, $document)
    foreach ($words as $word) {
        if (strpos($words, $word) !== FALSE) {
            return TRUE;
    return FALSE;

squiggles out a word Ah fixed it.