Ask a question about php search. for example, if I enter the keyword "sincerely", I can find the phrase "I have already given my heart to you". how can I use php to implement this? Please answer your questions.
Reply to discussion (solution)
It depends on what you use to implement it.
If it is a database, you can use like
It depends on what you use to implement it.
If it is a database, you can use like
Is to retrieve from a field in the database
It depends on what you use to implement it.
If it is a database, you can use like
The problem is that the keywords are split and retrieved in the fields including split. how can this problem be achieved with like?
Add tables and fields
Association id, keyword
When data is added and modified, php loops through your dictionary. if you have these words, put them in this table and search for this table.
Basic SQL fuzzy query
$ Ar = preg_split ("// u", 'true',-1, PREG_SPLIT_NO_EMPTY); $ where = array (); foreach ($ ar as $ v) $ where [] = "field like '% $ v %'"; $ SQL = "select * from tt"; $ SQL. = empty ($ where )? '': 'Where'. join ('and', $ where); echo $ SQL;
Upstairs writing is a little more complex, but General
In fact, it is to divide your keywords into LIKE
Where kword like '% true %' and kword like '% heart %' can be found only when both words match
Where kword like '% true %' or kword like '% heart %' can be found after matching one
$ Ar = preg_split ("// u", 'true',-1, PREG_SPLIT_NO_EMPTY); $ where = array (); foreach ($ ar as $ v) $ where [] = "field like '% $ v %'"; $ SQL = "select * from tt"; $ SQL. = empty ($ where )? '': 'Where'. join ('and', $ where); echo $ SQL;
Does this search all fields with "true" and all fields with "heart?
In fact, my expected result is: if this field only contains one "true" character, this field will not be searched, for example, "I really like", this field will not be found, or "I'm in a good mood" won't be found.
However, fields including "true" or "true" and "heart" will be searched.
For example, I really like it. This sentence will be found. Or the phrase "I have already given my heart to you" will also be found.
This problem occurs when it is used to search products on the office supplies sales website.
Upstairs writing is a little more complex, but General
In fact, it is to divide your keywords into LIKE
Where kword like '% true %' and kword like '% heart %' can be found only when both words match
Where kword like '% true %' or kword like '% heart %' can be found after matching one
The result is as follows:
Http://www.staples.cn/word/%E6%96%87%E4%BB%B6%E9%80%8F%E6%98%8E
Add tables and fields
Association id, keyword
When data is added and modified, php loops through your dictionary. if you have these words, put them in this table and search for this table.
Unrealistic. this font is too huge.
$ Ar = preg_split ("// u", 'true',-1, PREG_SPLIT_NO_EMPTY); $ where = array (); foreach ($ ar as $ v) $ where [] = "field like '% $ v %'"; $ SQL = "select * from tt"; $ SQL. = empty ($ where )? '': 'Where'. join ('and', $ where); echo $ SQL;
Does this search all fields with "true" and all fields with "heart?
In fact, my expected result is: if this field only contains one "true" character, this field will not be searched, for example, "I really like", this field will not be found, or "I'm in a good mood" won't be found.
However, fields including "true" or "true" and "heart" will be searched.
For example, I really like it. This sentence will be found. Or the phrase "I have already given my heart to you" will also be found.
This problem occurs when it is used to search products on the office supplies sales website.
Why not give it a try first?
$ Ar = preg_split ("// u", 'true',-1, PREG_SPLIT_NO_EMPTY); $ where = array (); foreach ($ ar as $ v) $ where [] = "field like '% $ v %'"; $ SQL = "select * from tt"; $ SQL. = empty ($ where )? '': 'Where'. join ('and', $ where); echo $ SQL;
Does this search all fields with "true" and all fields with "heart?
In fact, my expected result is: if this field only contains one "true" character, this field will not be searched, for example, "I really like", this field will not be found, or "I'm in a good mood" won't be found.
However, fields including "true" or "true" and "heart" will be searched.
For example, I really like it. This sentence will be found. Or the phrase "I have already given my heart to you" will also be found.
This problem occurs when it is used to search products on the office supplies sales website.
Why not give it a try first?
I tried it and it is available. thank you very much.