Replace Exact Occurrence of Word in PHP? -


i need repeatedly remove stop words articles. using function str_replace achieve this. first argument use stop list array variable remove occurrence of stop words. works fine except removes matches occur in middle of word (i.e, if stop words "th" remove "th" "the", "then" etc).

now if have supplied argument using plain text add space on either side of word remedy situation. since using variable array won't work. tried using concatenate operator, doesn't seem legal connector inside function.

current code looks this:

$i = str_replace(" " . $swarray . " ", $string ); 

you need instead use preg_replace word boundaries. example below we're replacing word the while avoiding replacing them or then etc

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

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -