Re: Check if string contains matched words.
- From: Leigh Finch <feiyang83@xxxxxxxxx>
- Date: Sat, 24 May 2008 20:55:15 +1000
Eureka22 wrote:
Hi,
I'm looking for a script that checks if there are any words in a string that
match with words in a file.
Suppose I have a file filter.txt containing the words: white, green, red
And a string: "This apple is yellow".
The result should be:
echo "No match found";
Can someone help me with a piece of script to help me?
Regards.
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
.
- Follow-Ups:
- Check if string contains matched words.
- From: Eureka22
- Check if string contains matched words.
- References:
- Check if string contains matched words.
- From: Eureka22
- Check if string contains matched words.
- Prev by Date: Check if string contains matched words.
- Next by Date: Check if string contains matched words.
- Previous by thread: Check if string contains matched words.
- Next by thread: Check if string contains matched words.
- Index(es):
Relevant Pages
|