Results 1 to 5 of 5

Thread: keywords...

  1. #1
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default keywords...

    Hi,

    I want to be able to save the txt entered from a text box/form into a mysql database, and then be able to search for keywords in the text entered, and display an appropriate message..

    e.g.

    Data entered into form:
    "Thank you very much for all your help"

    Keyword - Responce:
    thank - Your Welcome
    help - Need more help?


    All the keywords and the responces will be saved in a database..

    Thanks!

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Ok, i now have a database (keydb) with the list of keywords, and responses.. and also a page that saves the txt that was input, in another database (txtdb)

    Database scructures:

    keydb:
    keyword | responce

    txtdb:
    id | txt

    I want to be able to search the txt to see if there are any keywords in them, and if any, show the responce. or just say "try again"

    Cheers for your help.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    PHP Code:
    <?php
    $text 
    //this is your input text
    $keywords //this should have been retrieved some the DB as the keywords, numerically ($keywords[0], [1], ... etc.)
    $responses //same, but for responses

    foreach ($keywords as $keyword) {
    if (
    strpos($text,$keyword)) {
      
    $matches[$matchn] = $responses[$n];
      
    $matchn++;
      }
    $n++;
    }

    print_r($matches); //prints the array of matches
    //do a foreach loop with echo or whatever you'd like instead

    ?>
    One alternative is to search for ' '.$keyword.' ' instead, so you know there are spaces on either side of the word. Then you get into the complexities of things like "keyword.", etc.

    As it is now, though, if a keyword is car, then both racecar and car will return true for that.

    An easy option (though might run into problems if you do complex things later) is to use:
    $text = str_replace('.','',$text);
    for each character that isn't relevant. Since "." will just be at the end of a search and probably get in the way for finding matches, getting rid of it (and ";" "," etc) will be a good idea.

    Then just use ' '.$keyword.' ' as the search term above, meaning search for [space]keyword[space] within the text.


    And... well....
    else { echo 'try again'; }
    you'd just need to setup an appropriate if statement, such as if (isset$matches)).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •