3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = "Each person wants peaches for themselves forever"; $blacklist = array("for", "each"); //print_r($blacklist); // if you might have non-letter characters that have special meaning to the regex engine //$blacklist = array_map(function($v){return preg_quote($v, '/');}, $blacklist); //print_r($blacklist); echo "Without wordboundaries:\n"; var_export(preg_replace('/' . implode('|', $blacklist) . '/i', '', $string)); echo "\n\n---\n"; echo "With wordboundaries:\n"; var_export(preg_replace('/\b(?:' . implode('|', $blacklist) . ')\b/i', '', $string)); echo "\n\n---\n"; echo "With wordboundaries and consecutive space mop up:\n"; var_export(trim(preg_replace(array('/\b(?:' . implode('|', $blacklist) . ')\b/i', '/ \K +/'), '', $string)));

preferences:
24.97 ms | 407 KiB | 5 Q