What are best practices for handling comments within strings when using preg_match_all in PHP?

When using preg_match_all in PHP to extract content from strings, it's important to handle comments within the strings properly to avoid any interference with the regular expression pattern. One common approach is to use the "s" modifier in the regex pattern, which allows the dot metacharacter to match newline characters as well. This ensures that comments within strings are also captured correctly.

// Example code snippet demonstrating how to handle comments within strings using preg_match_all

$string = 'This is a string with a comment /* This is a comment */ and another comment /* Another comment */';
$pattern = '/\/\*.*?\*\//s'; // Regex pattern to match comments within strings
preg_match_all($pattern, $string, $matches);

print_r($matches[0]); // Output the matched comments within strings