how to remove words from string in php code example
Example 1: php remove text from string
<?php
echo str_replace("world","Peter","Hello world!");
?>
Example 2: php remove stop words from string
public function optimizeSearchString($searchString = "")
{
$stopwords = array(
'der' => 1,
'die' => 1,
'das' => 1,
'the' => 1);
$words = preg_split('/[^-\w\']+/', $searchString, -1, PREG_SPLIT_NO_EMPTY);
if (count($words) > 1) {
$words = array_filter($words, function ($v) use (&$stopwords) {
return !isset($stopwords[strtolower($v)]);
}
);
}
if (empty($words)) {
return $searchString;
}
return implode(" ", $words);
}