Highlight keywords in a paragraph

前端 未结 7 1604
梦如初夏
梦如初夏 2020-11-29 13:40

I need to highlight a keyword in a paragraph, as google does in its search results. Let\'s assume that I have a MySQL db with blog posts. When a user searches for a certain

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 14:35

    If you're a beginner this will not be super easy as someone might think...

    I think you should do the following steps:

    1. build a query based on what user searched (beware of sql injections)
    2. fetch the results and organize them (an array should be fine)
    3. build the html code from the previous array

    In the third step you can use some regular expression to replace the user searched keywords with a bolded equivalent. str_replace could work too...

    I hope this helps... If you could provide your database structure maybe I can give you some more precise hints...

提交回复
热议问题