PHP - Find if any of the keywords in an array exist in a string

前端 未结 5 1714
死守一世寂寞
死守一世寂寞 2021-02-06 19:32

Basically, I have an array of keywords, and a piece of text. I am wondering what would be the best way to find out if any of those keywords are present in the text, bearing in m

5条回答
  •  爱一瞬间的悲伤
    2021-02-06 20:17

    I really don't know if it is more efficient, but you could try to put them all in a regex like this: (keyword1|keyword2|...) With the preg_quote function you can escape the keywords for the regex. If you set the compiled option, it might be more efficient when using it with multiple strings.

提交回复
热议问题