regex to replace a given word with space at either side or not at all

前端 未结 4 1053
后悔当初
后悔当初 2021-01-21 18:48

I am working with some code in PHP that grabs the referrer data from a search engine, giving me the query that the user entered.

I would then like to remove certain stop

4条回答
  •  逝去的感伤
    2021-01-21 19:36

    You can use word boundaries for this

    $keywords = preg_replace('/\bfor\b/', '', $keywords);
    

    or with multiple words

    $keywords = preg_replace('/\b(?:for|sale)\b/', '', $keywords);
    

提交回复
热议问题