How can regular expressions be used to search for and remove PHP code from a text variable in PHP?

Regular expressions can be used to search for and remove PHP code from a text variable in PHP by using the `preg_replace()` function. You can define a regular expression pattern that matches PHP code, and then use `preg_replace()` to replace any matches with an empty string. This will effectively remove the PHP code from the text variable.

<?php
$text = "This is some text with <?php echo 'PHP code'; ?> in it.";
$cleaned_text = preg_replace("/<\?php(.*?)\?>/s", "", $text);
echo $cleaned_text;
?>