慕神8447489
解决此问题的一种方法是将成分列表拆分为逗号,然后过滤掉以开头的值,no然后再次内爆列表。这样做的好处是不会在输出中留下悬挂的逗号:function filter_ingredients($elements) { return implode(', ', array_filter(preg_split('/,\s*/', $elements), function ($v) { return !preg_match('/^no\b/', $v); }));}例如:echo filter_ingredients("cheese, no tomato, no onion, mayo, no lettuce") . "\n";echo filter_ingredients("no cheese, tomato, no onion, no mayo, lettuce") . "\n";echo filter_ingredients("no cheese, no tomato, onion, no mayo, no lettuce") . "\n";输出:cheese, mayotomato, lettuceonion3v4l.org 上的演示请注意,此代码假定您的成分中没有逗号。