PHP eval - Execute php script in a string

Do you want to be able to run a php script in a string ?
This could be very helpful, for example if you want to use a module or a template, you may have a php script in a string, just like that:
Hello echo $username; ?> . You are wellcome !
What if you have this in a text file ? or in a database ? What should you do ?
If you loads that text in a string, and echo it to the html page, it will not be executed by php.
<?php
$string = "
Hello ?> . You are wellcome !";
echo $string;
?>
Display:
Hello ?> . You are wellcome !
So how to make php execute it ?

If you have only a php script in a string, you just use eval() to execute:
<?php
$string = "
echo $username; ";
eval ( $string ) ;
?>
This is the same thing than:
<?php
echo $username;
?>
If you have a string that is a html with some php tags, like the first example, do the following:
<?php

$username = "SMITH" ;
$string = "Hello . You are wellcome !";

// This will search for the php script in the string:
$pos = strpos( $tmp ,"" ) ;
$pos2 = strpos ( $tmp , "?>" , $pos ) ;
$scr = substr($tmp,$pos + 5 , $pos2 - $pos -3 ) ;

//This will execute the script.
echo substr($tmp,0,$pos);
eval( " $scr " );
echo substr($tmp,$pos2+2);


?>

Display:
Hello SMITH . You are wellcome !