What are the best practices for extracting meta tags from HTML content using regular expressions in PHP?

Extracting meta tags from HTML content using regular expressions in PHP can be achieved by using the preg_match_all function with a specific regex pattern to match meta tags. It is important to use a regex pattern that captures the meta tag name and content accurately. Additionally, it is recommended to sanitize the extracted meta tag values to prevent any potential security vulnerabilities.

$html = "<html><head><meta name='description' content='This is a sample description'><meta name='keywords' content='sample, keywords'></head></html>";

$pattern = '/<meta\s+name=[\'"]([^\'"]+)[\'"]\s+content=[\'"]([^\'"]+)[\'"]/i';

preg_match_all($pattern, $html, $matches);

$meta_tags = array_combine($matches[1], $matches[2]);

print_r($meta_tags);