5

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)

10
  • 1
    Why do you want to do this? It smells like a bad idea. Commented Jul 1, 2010 at 1:57
  • 1
    The answer is you do not use eval. ;) (Only 25% joking actually.) Commented Jul 1, 2010 at 1:58
  • 3
    I used eval once, puppies died, true story. Commented Jul 1, 2010 at 1:59
  • trust me, this is for a very legitimate reason :) Commented Jul 1, 2010 at 2:01
  • 2
    eval() is evil.. it can lead to all kinds of vulnerabilities as you don't know if it is scrubbing variables it uses; accessing things on the filesystem, web, etc; doing things with the session or cookies; or killing puppies. Bad. Commented Jul 1, 2010 at 4:06

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I think he only wants the start and end ones removed, but I may be wrong.
Yep, I didn't see that. I thought he was having trouble with different kinds of tags.
4

Sounds like a bad idea, but if you want the start and end ones removed you could do

$removedPhpWrap = preg_replace('/^<\?php(.*)(\?>)?$/s', '$1', $phpCode);

This should do it (not tested).

Please tell me also why you want to do it, I'm 95% sure there is a better way.

3 Comments

what about php without the last '?>' ? :P
Read this answer here before using above solution: stackoverflow.com/a/1732454/589909
@brenjt So parsing HTML with regex and matching PHP opening and closing tags are the same thing? TIL.
3

You could do:

eval("?> $string_to_be_evald <?");

which would close the implicit tags and make everything work.

Comments

1

There's no need to use regex; PHP provides functions for removing characters from the beginning or end of a string. ltrim removes characters from the beginning of the string, rtrim from the end of the string, and trim from both ends.

$code = trim ( $code );
$code = ltrim( $code, '<?php' );
$code = rtrim( $code, '?>' );

The first trim() removes any leading or trailing spaces that are present in the siting. If we omitted this and there was whitespace outside of the PHP tags in the string, then the ltrim and rtrim commands would fail to remove the PHP tags.

8 Comments

The problem with this solution is that the <? ?>-tags can be present several times in the file, not just at the beginning and end. Actually, if you look at his sample, there are two sets of tags in there. So regex is probably the most viable solution here.
@Karl-JohanSjögren The functions I've used only removes PHP tags from the beginning or end of the string; not just anywhere in the string. You can read more about these functions at the links I've included in my answer
And if you do that, someone can sneak a third set of tags in the middle of the string and still do bad stuff, so this really isn't a good recommendation for the askers case.
Huh? If you read the OP's comments, he says that only the administrator can edit what is passed to eval(). Anyway, how exactly would a third set of tags be bad?
Plus, the regex method suggested here doesn't work if the string has a newline after the closing ?> tag, or an leading space in front of the first <?php tag (or any other whitespace surrouding those tags)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.