Re: Check if string contains matched words.



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
.



Relevant Pages

  • Re: How to rewrite with awk?
    ... > I'm unfamiliar with tools such as sed & awk. ... Extract the string that matches a RE. ... This script will not only expand all the lines that say "include ... file) and not resetting ARGV(the tmp file), it then lets awk do any ...
    (comp.unix.shell)
  • Re: Slow string
    ... I see virtually no difference in execution time for the code you posted when using cscript versus using wscript; to confirm, I bracketed the code with an initial ... Are you actually entering the script name into a console window? ... You can use the exact same line for every wrapper script - and that includes wsf and js scripts; all you need to do is change the final "vbs" to "wsf" or "js" as appropriate. ... If you build a large string by small concatenations, the performance degrades geometrically - so does array resizing, because they both use the same nasty technique inherited from VB1 of copying the entire data structure to a new structure with the added element. ...
    (microsoft.public.scripting.vbscript)
  • Re: How do I remove filenames with spaces from a shellscript.
    ... >> The above script cannot handle filenames with spaces, the for loop sees every string ...
    (comp.unix.shell)
  • Re: Function that returns date of file.
    ... string after the date/time when it is used by itself. ... Is that your entire script? ... I make an IF statement that required the 'equals equals'. ... designed database your job will be all that much harder. ...
    (alt.php)
  • Re: Script: Remote shutdown of all domain computers
    ... When trying to run the script from a XP workstation, where i am logged in as ... Dim strBase, strFilter, strAttributes, strQuery, objRecordSet ... 2000 or above and allow shutdown, ... Public Function AllComputersAs String() ...
    (microsoft.public.windows.server.scripting)