PHP Parsing text field to find tags
I'm creating an online FAQ app for my company using PHP.
To populate the database with an FAQ the user must enter the Question, the Answer and some search Tags.
The user s开发者_Go百科earches the system by entering their question into a text field. Does anyone know of any PHP (or JavaScript) modules that can analyse the question to find keywords for use in an SQL search?
For example in the question - "What is the company internet policy" the bold items would be identified as keywords so they could then be searched for in the database?
If you want to use particular/selected keywords for searches, asking the user to input search tags is, in my opinion, unnecessary. I'd create two tables: one to contain your chosen keywords (which would only contain an indexed PRIMARY column - which I'll call keyword
) and another which stores all your FAQ information. The PHP would then use the user's text string to search the keywords table:
$string = $_POST['search_string'];
$this->db->like('keyword',$string);
$search = $this->db->get('keywords');
if($search->num_rows() > 0){
$keyword = $search->keyword;
$this->db->like('faq_content',$keyword);
$search_faq = $this->db->get('faqs');
foreach($search_faq->result() as $faq){
echo "<h2>$faq_title</h2><p>$faq_content</p>";
}
}
Make sure you sanitize your search_string value before letting it go near your database.
Assuming MySQL, add FULLTEXT
indexing on the relevant columns and use a natural language full-text search.
You should then be able to use the entered title in the AGAINST
clause, eg
SELECT id, title, body, MATCH (title, body) AGAINST
('What is the company internet policy') AS score
FROM faq WHERE MATCH (title, body) AGAINST
('What is the company internet policy');
精彩评论