I need to replace a string that contains <?php echo $test; ?>
with "Hello world
".
strpos($string_with_php_tags,'<?php echo $test; ?>') !== false) // <-- not working
$hello = str_replace('<?php echo $test; ?>', 'hello world', $string_with_php_tags); // not working.
Is this possible with PHP? I couldn't find a way to escape from this. Perhaps a regex?
Thanks Jorge.
CodePudding user response:
Try this Regex
Just some backslashes on some characters does the trick
/<\?php .*; \?>/
<?php
$str = "<?php $test; ?>";
$pattern = "/<\?php .*; \?>/";
$ans = preg_replace($pattern, 'Hello world', $str);
echo $ans;
?>
Output: Tell me if its not working...