What are the best practices for searching for PHP code within a mixed HTML and PHP file using regular expressions in PHP?

When searching for PHP code within a mixed HTML and PHP file using regular expressions in PHP, it is important to consider the structure of the file and the patterns of PHP code you are looking for. One approach is to use regex to match PHP opening and closing tags along with the code in between. This can help accurately identify PHP code blocks within the file.

$file_contents = file_get_contents('your_file.php');
preg_match_all('/<\?php(.*?)\?>/s', $file_contents, $matches);

foreach ($matches[0] as $match) {
    echo $match . "\n";
}