How to full-text search database, matching keywords?

I want to re-manage my articles in my database and categorize them. I pre-set some tag words in the database. How to write a PHP code to facilitate analyze the articles for match the keywords in the TAG of the database? and then output the TAG of each articles? Thank you.

database name ‘food’ the structure as the follow:
id name1 name2 name3 name4 type
1 fruit apple red 2008 article
2 fruit banana yellow 2007 article
3 drink beer yellow 2008 article
4 fruit apple yellow 2008 topic

there are many full-text, one of those like:


require_once('conn.php');
$txt='<div id="hear">article</heard><div class="left"><p>[color=#00FF00]Banana[/color] and longevity. Folk physicians of ancient Persia and India regarded this golden [color=#00FF00]fruit[/color] as natures secret of perpetual youth.</p></div><div class="right"><p>The [color=#FF0000]apple[/color] is the pomaceous [color=#FF0000]fruit[/color] of the apple tree, species Malus domestica in the rose family (Rosaceae) and is a perennial. It is one of the most widely.</p></div>';

If <div class=“right”> has some words match the database fields “name1” and “name2”, echo the resault: fruit apple red 2008.


require_once('conn.php');
$txt='<div id="hear">topic</heard><div class="left"><p>Banana and longevity. Folk physicians of ancient Persia and India regarded this golden fruit as natures secret of perpetual youth.</p></div><div class="right"><p>The apple is the pomaceous food  of the apple tree, species Malus domestica in the rose family (Rosaceae) and is a perennial. It is one of the most widely.</p></div>';

If <div class=“right”> has not words match all the database fields “name1” and “name2”, analyze the <div id=“hear”> and macth database fields “name2” “type” echo the resault: fruit apple yellow 2008.

Thanks Egyptechno, but what is ‘blah’ and ‘boom’? Please forgive my stupidity.

select … from table where name1 = ‘blah’
select … from table where name3 = ‘blah’ AND name2 = ‘boom’

ok, let’s have a look first on pattern u should b using on regex (regular experssion)

you have this :


<div id="hear">article</heard><div class="left"><p>Banana and longevity. Folk physicians of ancient Persia and India regarded this golden fruit as natures secret of perpetual youth.</p></div><div class="right"><p>The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae) and is a perennial. It is one of the most widely.</p></div>

if you use :


<?php
$text = " ....... "; //text above the div
preg_match("/<div id=\\"hear\\">article<\\/heard><div class=\\"left\\"><p>(.*)<\\/p><\\/div>/is", $text, $result);
print_r($result);
?>

i guess and supposed when u run this code … you get all text indicated between div tags … now you got it ? you have to find indicators to catch “Banana” and " fruit "

if you did … db queries will be easy … it’s not that hard and yet you can still use basic sql query …

select … from table where name1 = ‘blah’
select … from table where name3 = ‘blah’ AND name2 = ‘boom’

so, you just need regular expression to be done … and then it’s easy ( actually RegEx is yet easy … )

Thanks Egyptechno, I try to read the tutorial, but it is difficult to be understand with some db queries.
I try to use mysql_query and I posted a second question. But the admin moved my second question here.
Can I push it up? And waiting for help?
Thanks.

database name ‘food’ fields as below:
id name1 name2 name3 name4 type
1 fruit apple red 10 article
2 fruit banana yellow 12 article
3 drink beer yellow 6 article
4 fruits apple yellow 16 books

<?
mysql_connect(“localhost”, “root”, “root”) or die(mysql_error());
mysql_select_db(“article”) or die(mysql_error());
… …
echo $match . “\r”; // $match= This is a apple, it is a fruit
$query = mysql_query("SELECT * FROM food WHERE name1, name2 like ‘%$match%’ ");
$row=mysql_fetch_array($query);
echo $row['id ']. “\r” .$row[‘name1’]. “\r”.$row[‘name2’]. “\r”.$row[‘name3’]. “\r”.$row[‘name4’]. "
";
?>
I want insert “This is a apple, it is a fruit” to query the database. and only match the words ‘apple’ and ‘fruit’ not ‘fruits’ from the database and return 1 fruit apple red 10.
How to modify the query code?
Thanks.

Try take a look at http://www.sphider.eu/
I have used it and you tell it when to crawl your pages and it sets up a very nice search engine. It’s nice to build your own but sphider is taking the easy way out.

blah = BANANA
boom = fruit

and it should b in variables should b like this :
$result[1][0]
$result[2][0]

or something like that … depends on ur pattern, u get result in $result array

you would use

www.php.net/preg_match

  • some db queries … you may consider db performance and enhancement

otherwise get a freelancer to have it done for you