views:

104

answers:

1

I have been trying to make this search engine for a MySQL database. Taking in user input is no problem, database querying is also fine.

One thing I need to figure out is this:

I am a string in the database

How do I match the input "AM", but keep the same case? There are PHP functions like str_ireplace or preg_replace/eregi_replace, but what I need to do is split the string according to every exact match (as separate word, so do not match "lAMb", only "am").

That is so I may highlight the matches. Also I need to figure out how to dynamically shorten a string but keep where matches are in the middle:

... this is part of the string where ...

and do this according to font-size set by user in any browser. I had one which only worked in WebKit, but this is no good. Please answer with something like PHP or Python, I'm not a hard-core C kind of guy.

+4  A: 

I suggest you read this

http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html

It may help you build a better search engine and probably answer some part of your question

Here is a good example of what you could do :

mysql> SELECT id, body, MATCH (title,body) AGAINST
    -> ('Security implications of running MySQL as root'
    -> IN NATURAL LANGUAGE MODE) AS score
    -> FROM articles WHERE MATCH (title,body) AGAINST
    -> ('Security implications of running MySQL as root'
    -> IN NATURAL LANGUAGE MODE);
    +----+-------------------------------------+-----------------+
    | id | body                                | score           |
    +----+-------------------------------------+-----------------+
    |  4 | 1. Never run mysqld as root. 2. ... | 1.5219271183014 |
    |  6 | When configured properly, MySQL ... | 1.3114095926285 |
    +----+-------------------------------------+-----------------+
    2 rows in set (0.00 sec)
Philippe