How can regular expressions be used in PHP to exclude certain patterns when searching within a string?

To exclude certain patterns when searching within a string using regular expressions in PHP, you can use negative lookahead assertions. This allows you to specify patterns that should not be matched in the search results. By using this technique, you can effectively exclude specific patterns from the search results.

$string = "The quick brown fox jumps over the lazy dog";
$pattern = '/\b(?!brown|lazy)\w+\b/';

if (preg_match_all($pattern, $string, $matches)) {
    foreach ($matches[0] as $match) {
        echo $match . "\n";
    }
}