How can regular expressions be used in PHP to extract specific data from HTML tags?

Regular expressions can be used in PHP to extract specific data from HTML tags by using the preg_match() function. By creating a regular expression pattern that matches the desired data within the HTML tags, you can extract the data using this function. This can be useful for parsing HTML documents and extracting specific information, such as text within specific tags or attributes.

$html = '<div class="example">Hello, World!</div>';
$pattern = '/<div class="example">(.*?)<\/div>/';
preg_match($pattern, $html, $matches);
echo $matches[1]; // Output: Hello, World!