Replace Exact Occurrence of Word in PHP?

后端 未结 3 1561
耶瑟儿~
耶瑟儿~ 2021-01-14 18:46

I need to repeatedly remove certain stop words from articles. Currently I am using the function str_replace to achieve this. As the first argument I use the stop list array

3条回答
  •  一生所求
    2021-01-14 19:49

    You need to instead use preg_replace with word boundaries. For example below we're only replacing word the while avoiding replacing them or then etc

    $string = preg_replace('/\bthe\b/', '', $string);
    

提交回复
热议问题