How can regular expressions be used to search for patterns in PHP?

Regular expressions can be used in PHP to search for specific patterns within strings. By using functions like preg_match() or preg_match_all(), you can specify a regular expression pattern to search for within a string. This allows for more complex and flexible searching capabilities compared to simple string functions.

$string = "Hello, World! This is a test string.";
$pattern = '/\b\w{5}\b/'; // Search for words with 5 characters

if (preg_match_all($pattern, $string, $matches)) {
    echo "Matches found: ";
    print_r($matches[0]);
} else {
    echo "No matches found.";
}