Using a regular expression to remove php code from a string fails when the string includes the characters ‘?>’.
For example:
[code=php]<?php
// works beautifully –
// displays: ##### Here is some text. #####
echo preg_replace(‘/<?(.*?)?>/s’,’#####’,”<?php echo ‘TEST1’; ?> Here is some text. <?php echo ‘morestuff’; ?>”);
// reads the ? > in the string and breaks off –
// displays: Preceding text.##### tag in here. Oh no!’; return myFunc2(); ? > Uh oh!
// desired output: Preceding text.##### Uh oh!
echo preg_replace(‘/<?(.*?)?>/s’,’#####’,”Preceding text.<?php myFunc1(); echo ‘There is a ?> tag in here. Oh no!’; return myFunc2(); ?> Uh oh!”);
?>
Any ideas on how to prevent it from reading the ‘?>’ when it’s inside a string?
strip_tags() has the exact same issue, in addition to removing ALL tags instead of just PHP:
[code=php]<?php
// displays: tag in here. Oh no!’;? >
echo strip_tags(“<?php echo ‘There is a ?> tag in here. Oh no!’;?>”);
?>