seval

seval
SYNOPSIS

seval($s)

DESCRIPTION

seval returns the result of the evaluation by PHP of $s.

CODE
  1. function seval($s) {
  2.     global $base_path, $base_url, $base_root;
  3.  
  4.     ob_start();
  5.     echo eval('?>'. $s);
  6.     return ob_get_clean();
  7. }

Adding the closing tag ?> at the beginning of the text protects from a tag <?php left opened in $s and garantees that all the code between the tags <?php and ?> contained in $s will be evaluated.

Declaring the global variables $base_path, $base_url and $base_root places them automatically in the context of the code which is being evaluated.

EXAMPLE
$ php -a
php> require_once 'library/seval.php';
php> $text='<p>1 + 1 = <?php echo 1+1; ?></p>';
php> echo seval($text);
<p>1 + 1 = 2</p>
php> quit

Comments

To add a comment, click here.