从字符串中替换一些单词

I want to remove some words from a string:

  1. the word: fa
  2. the word: livicon
  3. all words starting with fa- (in this case fa-word and fa-word-small, but this words can also be different. For example fa-user)

String:

fa fa-word livicon fa-word-small fantastic

Output should be

fantastic

You want to use preg_replace() with a regular expression that uses word boundaries and an optional grouping for the -word-etc parts of your prefixed strings. The below would suffice ...

$str = 'fa fa-word livicon fa-word-small fantastic';
$str = preg_replace('~\b(?:fa(?:-\w+)*|livicon)\b~', '', $str);
echo trim($str); //=> "fantastic"