开发者

identify and execute php code on a string

开发者 https://www.devze.com 2023-03-07 17:53 出处:网络
I would like to know if it\'s possible to execute the php code in a string. I mean if I have: $string = If i say <?php echo \'lala\';?> I wanna get \"<?php echo \'dada\'; ?>\";

I would like to know if it's possible to execute the php code in a string. I mean if I have:

$string = If i say <?php echo 'lala';?> I wanna get "<?php echo 'dada'; ?>";

Does anybody knows how?

[EDIT] It looks like nobody understood. I wanna save a string like

$string = If i say <?php count(array('开发者_StackOverflow社区lala'));?>

in a database and then render it. I can do it using

function render_php($string){
    ob_start();
    eval('?>' . $string);
    $string = ob_get_contents();
    ob_end_clean();
    return $string;
}

The problem is that I does not reconize php code into "" (quotes) like

I say "<?php echo 'dada'; ?>"


$string = ($test === TRUE) ? 'lala' : 'falala';

There are lots of ways to do what it looks like you're trying to do (if I'm reading what you wrote correctly). The above is a ternary. If the condition evaluates to true then $string will be set to 'lala' else set to 'falala'.

If you're literally asking what you wrote, then use the eval() function. It takes a passed string and executes it as if it were php code. Don't include the <?php ?> tags.

function dropAllTables() {
    // drop all tables in db
}

$string = 'dropAllTables();';

eval($string); // will execute the dropAllTables() function

[edit]

You can use the following regular expression to find all the php code:

preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);

$php_code will be an array where $php_code[0] will return an array of all the matches with the code + <?php ?> tags. $php_code[2] will be an array with just the code to execute.

So,

$string = "array has <?php count(array('lala')); ?> 1 member <?php count(array('falala')); ?>";
preg_match_all('/(<\?php )(.+?)( \?>)/', $string, $php_code, PREG_OFFSET_CAPTURE);
echo $php_code[0][0][0]; // <?php count(array('lala')); ?>
echo $php_code[2][0][0]; // count(array('lala'));

This should be helpful for what you want to do.


Looks like you are trying to concatenate. Use the concatenation operator "."

$string = "if i say " . $lala . " I wanna get " . $dada;

or

$string = "if i say {$lala} I wanna get {$dada}.";

That is what I get since your string looks to be a php variable.

EDIT: <?php ?> is used when you want to tell the PHP interpreter that the code in those brackets should be interpreted as PHP. When working within those PHP brackets you do not need to include them again. So as you would just do this:

// You create a string:
$myString = "This is my string.";

// You decide you want to add something to it.
$myString .= getMyNameFunction(); // not $myString .= <?php getMyNameFunction() ?>;

The string is created, then the results of getMyNameFunction() are appended to it. Now if you declared the $myString variable at the top of your page, and wanted to use it later you would do this:

<span id="myString"><?php echo $myString; ?></span>

This would tell the interpreter to add the contents of the $myString variable between the tags.


Use token_get_all() on the string, then look for a T_OPEN_TAG token, start copying from there, look for a T_CLOSE_TAG token and stop there. The string between the token next to T_OPEN_TAG and until the token right before T_CLOSE_TAG is your PHP code.

This is fast and cannot fail, since it uses PHP's tokenizer to parse the string. You will always find the bits of PHP code inside the string, even if the string contains comments or other strings which might contain ?> or any other related substrings that will confuse regular expressions or a hand-written, slow, pure PHP parser.


I would consider not storing your PHP code blocks in a database and evaluating them using eval. There is usually a better solution. Read about Design Pattern, OOP, Polymorphism.


You could use the eval() function.

0

精彩评论

暂无评论...
验证码 换一张
取 消