0

I have code that only partially works; it will find only the 'single' string from a given other string in a search. ie; the text in a string is "red green blue", I am trying to 'compare? or 'match'? it with a small file that has single words only..

The problem is if I type a Single word that 'matches' one in the file it is okay.

I would like it to use the entire 'phrase' in the string to match from any place the word is located. Such as if I type, "red green blue".. it finds the "green" which IS listed in the $textfile, whereas "red" and "blue" are Not.

As it is, if i type "green".. it works fine. if I type "red green blue" it does not 'find' anything.

I have tried, have been trying; stristr functions, imploding the string, etc..

Here is what I have:

$search=$input; //example as above "red green blue" would be the $input
$lines = file($textfile); //this is the File that contains 20 single words, 
                     //"green" is one of them, "red" and "blue" *are not*.
foreach($lines as $line)
{
// Check if the line contains the string in $input
if(stristr($line, $search) !== false)
$answer=$line;
}
if(!$found)
{

after that it I would like to have the $answer result in "green" It is not an issue, UNLESS the String is within other $input "texts". (mentioned)

Thank you for any help.

I made this edit to "split" the input.. it is still not working as I would hope. It basically works the same way .. Any further suggestions?

 $search=$input; 

 foreach(preg_split("/((\r?\n)|(\r\n?))/", $input) as $inputs) 

 $lines = file($textfile); 
 foreach($lines as $line) { 
 if(stristr($line, $inputs) !== false) 
 $answer=$inputs; 
 } 
 if(!$found) 
 {
GX1705
  • 3
  • 2
  • So split your search input text into the individual words then, and loop over them …? – CBroe May 25 '20 at 11:47
  • I'll give that a shot, when i can find the method. thanks. – GX1705 May 25 '20 at 12:14
  • not having much luck.. i found some example perhaps, but i do not know how to implement them.. i get what youre saying though, makes sense. I am trying to use the most common method with 'for($i =1; $i++)' ? not sure i have that even right. maybe someone can explain, or point the direction/means to achive this somehow. i have also been trying a preg_split, with failing results. thanks for any help. – GX1705 May 25 '20 at 20:24
  • This is hardly readable in comments. Please edit your question and add it to the end, properly formatted. – CBroe May 27 '20 at 06:31
  • Sorry about that. I have updated the post. @CBroe – GX1705 May 28 '20 at 03:32
  • I think that the preg_split i have listed using the "/((\r?\n)|(\r\n?))/" is incorrect. the input is in a 'phrase' not in a list-like (basically wouldnthave linefeeds or carriage returns).. like in a comment.. there will be spaces between the input words.. Thanks. – GX1705 May 28 '20 at 03:49
  • EDIT: I have now been able to use this to work (almost) "/[\s,]+/" .. it will detect the 'inputtext' i am looking for, *but only if it is either alone, or at the very end of the 'inputtext' string.. i need it to be detected anywere within the input text string.. if you could suggest the best preg_match I would greatly appreciate – GX1705 May 28 '20 at 04:02
  • `[\s,]+` still demands at least one character out of that class - which does not exist before the word, if it is a the very start of the line. If all the phrases you need to search for are real _words_, using a word boundary might make more sense. – CBroe May 28 '20 at 06:25

0 Answers0