Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Removing <?php ?> tags from a string

What's the best way to remove these tags from a string, to prepare it for being passed to eval() ?

for eg. the string can be something like this:

<?php
  echo 'hello world';
  ?>
  Hello Again
  <?php

  echo 'Bye';
?>

Obviously str_replace won't work because the two php tags in the middle need to be there (the the 1st and the last need to be removed)

like image 806
Alex Avatar asked Jul 01 '10 01:07

Alex


1 Answers

Usually, you wouldn't want to pass a function to eval. If you're wishing to just remove the tags, string_replace would do the job just fine, however you might be better off using a regex.

preg_replace(array('/<(\?|\%)\=?(php)?/', '/(\%|\?)>/'), array('',''), $str);

This covers old-asp tags, php short-tags, php echo tags, and normal php tags.

like image 112
CodeJoust Avatar answered Nov 07 '22 20:11

CodeJoust