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
This should work:
$i = $string;
foreach($swarray as $word) {
$i = str_replace(" " . $word . " ", "", $i );
}
preg_replace
with array
$find = array('/\bth\b/', '/\bthe\b/', '/\bthen\b/');
$replace = array('', '', '');
echo $i = preg_replace($find, $replace, $string);
$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
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);