Re: Check if string contains matched words.
- From: Leigh Finch <feiyang83@xxxxxxxxx>
- Date: Sat, 24 May 2008 22:14:34 +1000
Eureka22 wrote:
Hi,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"
My bad, forgot to trim line feeds off the end of the words to match. Try this.
<?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, rtrim($word)) !== false)
{
$match = true;
}
}
// Return the result
if($match == true)
{
echo("Match found");
}
else
{
echo("No match found");
}
?>
Hope this helps, Cheers
Leigh Finch
http://www.phpmaniac.net
.
- Follow-Ups:
- Re: Check if string contains matched words.
- From: J.O. Aho
- Check if string contains matched words.
- From: Eureka22
- Re: Check if string contains matched words.
- References:
- Check if string contains matched words.
- From: Eureka22
- Re: Check if string contains matched words.
- From: Leigh Finch
- 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
|