Check if string contains matched words.




Hi,
If the file contained each word on a new line, you could do something
like this (written, but not tested).

<?php
$match = false;
$string_to_match = "This apple is yellow";
$words = file("filter.txt");

// loop through each word in the file
foreach($words as $word)
{
if(strpos($string_to_match, $word) !== false)
{
$match = true;
}
}

// Return the result
if($match == true)
{
echo("Match found");
}
else
{
echo("No match found");
}
?>

$match contains whether a match has been found or not

$string_to_match is the string you want to search

$words is an array of each line in the the file filter.txt

strpos() is a function for search for the position of a string within a
string

Hope this helps, Cheers

Leigh Finch
http://www.phpmaniac.net


Thanks for your response. The script like this I.m looking for.
In the file each word is on a new line.
I tested the script, but in all cases I get "No match found"
.



Relevant Pages