Ok here's the deal. I want to make a script that can answer a question based on key-words. So if i put:

"whats the weather like today"

it would pick up the key words of "whats" "weather" "like"

and so i can have many questions, i want to make it into a virtual help asistant.

Thanks in advance.

    I assume that you want to get all words separated from each other and then match it against something.
    You can use explode() function to do that.

    $exp=explode(" ",$keyword);
    foreach ($exp as $value){
    //do what ever you wanna do here
    }
    

      Thanks, so how would i set the value

      $message = "the weather is cold today"

      only when the keywords = "what" and "weather"

        As for testing the string for the existence of key words, you could either use regex or the normal strpos() function. Regex gets a bit confusing, but it is way more powerful that most of the PHP string searching functions. Here is an example of using the strpos() function to figure out which answer to give:

        $input = 'whats the weather like';
        
        if (strpos($input, 'whats') &&
            strpos($input, 'weather') &&
            strpos($input, 'today'))
          {$message = $todaysWeatherData;}
        if (strpos($input, 'whats') &&
            strpos($input, 'weather') &&
            strpos($input, 'tomorrow'))
          {$message = $forcastWeatherData;}
        if (strpos($input, 'how') &&
            strpos($input, 'tie') &&
            strpos($input, 'shoes') ||
            (strpos($input, 'half') && strpos($message, 'hitch')))
          {$message = $commonKnots;}

        You can get pretty fancy with the logic, but it can also get cumbersome if you intend to create a virtual help assistant. I recommend reading up on PCRE (Perl Compatible Regular Expressions).

          Just started something similar myself. What I was thinking of doing was, keep a seperate text file of "useless" words- ie. the,an,a. Seperate the phrase into individual words, check each exploded term againist the text file and return the new array for searching.

          Good Luck.

            I may be missing something, but I really don't see the need to remove any words from the string before searching it for keywords. Nor do I see the need to explode the string and test each individual word. With strpos() or preg_match(), you can test the existence of words in a string even if the string is four sentences full of pronouns and other 'useless' words. You can even use those function to test word order (ie which words come first). It seems to me that exploding may be an unnecessary step.

            Just thought I'd mention it.

              So how would i d it then?? the input field is called "question"

              Can any one tell me, every thing i have tried doent work

              even this

              if($question = "whats you name"){
              $message = "My name is Juila"
              }

              But even taht no good ebcasue they have to type it exactly as it is stored.

              Please help

                change the code to something like this:

                $question = $_POST['question']; // get the form input into a variable
                if ((strpos($question, 'what') != FALSE) &&
                    (strpos($question, 'your') != FALSE) &&
                    (strpos($question, 'name') != FALSE)) // EDIT - 6/26/03 (corrected spelling)
                  {$message = "My name is Juila";}

                The if statement is checking to see if 'what', 'your', and 'name' are present in the string $question. The strpos() function will return the '0-based' position of the words you are looking for. For that reason you need to specify '!= FALSE' because if the word is at the very beginning of the string, it the strpos() function will return '0' which will cause the statement to fail. I forgot to mention that in one of my prior posts. (Note: you might want to use 'you' instead of 'your' because of variations in international english... not to mention that the 'r' is commonly forgotten when typing - I do it all the time!)

                  Nope, it still aint working. Its realy anoying why can it not manage a simple thung like this:

                  <?php
                  $question = $_POST['question']; // get the form input into a variable
                  if ((strpos($question, 'what') != FALSE) &&
                  (strpos($question, 'your') != FALSE) &&
                  (strpos($question, 'name') != FASLE))
                  {$message = "My name is Juila";}
                  if ((strpos($question, 'some_swear_word') != FALSE))
                  {$message = "Dont swear please";}
                  echo("$message");
                  ?>

                  Please help, my head hurts

                    Well, for one, I just noticed that in my previous post, I misspelled the third FALSE (as FASLE)... that isn't hanging you up is it? - I'll fix that after I post this...

                    What error are you getting? And what is the input and output?

                      Im not getting any errors atall. Just a blank page.

                      So if i type in "whats your name" it will just have a blank page.

                      Nothing happends??

                      Thanks

                        ... uh, need more info... You are using a form right? Is the form handler the same page or a different page? Are you sure that your form values are getting to your script? insert this line at the top to make sure:

                        echo $_POST['question'];

                        Is your field named "question"? The global POST array uses field names as indices. If your field is named 'input', then you need to use $_POST['input']. Is there more to your script?

                          try this in your help.php page:

                          <?php 
                          $message="";
                          $question = $_POST['question'];
                          
                          if (
                          (strpos($question, 'what') !== FALSE) &&
                          (strpos($question, 'your') !== FALSE) &&
                          (strpos($question, 'name') !== FALSE)
                          )
                          {
                          	$message = "My name is Juila";
                          }
                          if (strpos($question, "fek")!==FALSE)
                          {
                          	$message = "Dont swear please";
                          }
                          
                          echo "Message: ".$message;
                          ?>
                          
                          

                            Actua\ly the form does not appear to be getting the value from the feild?? Any idea's???????

                            I have used:

                            $_POST['question'];

                            and i am using post

                              What php version are you using?

                              If you are using a php version < 4.1.0 you have to use :

                              $question=$HTTP_POST_VARS["question"];

                              hth

                                You mentioned before that your form name is 'question'. For this purpose, it doesn't matter what the form name is (though if you use javascript, it's good to have it named). What's important is that the field name is 'question' (or whatever you have named the field, is what you want to referrence in the $_POST array. For example:

                                field name: question
                                use: $_POST['question']

                                field name: userName
                                use: $_POST['userName']

                                etc.

                                  Ok, i have fixed the problem it all works fine.

                                  I dont want to spam the forum so if any one wants the URL just ask.