Log in

View Full Version : keywords...



nikomou
02-11-2007, 06:56 PM
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!

mburt
02-11-2007, 07:11 PM
PHP/MySql tutorial:
http://www.freewebmasterhelp.com/tutorials/phpmysql

djr33
02-12-2007, 12:09 AM
or
http://php-mysql-tutorial.com

nikomou
02-13-2007, 01:18 PM
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.

djr33
02-14-2007, 01:29 AM
<?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)).