Replace Exact Occurrence of Word in PHP?

后端 未结 3 1573
耶瑟儿~
耶瑟儿~ 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:46

    preg_replace with array

    $find = array('/\bth\b/', '/\bthe\b/', '/\bthen\b/');
    $replace = array('', '', '');
    
    echo $i = preg_replace($find, $replace, $string);
    

    Or

    $find = array('/\bth\b/', '/\bthe\b/', '/\bthen\b/');
    
    echo $i = preg_replace($find, "", $string);
    

    Regex match document: http://www.php.net/manual/en/function.preg-replace.php#89364

    \b Match a word boundary
    

提交回复
热议问题