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 !


11 comments:

Cheap Guild Wars 2 Gold said...

it becomes an wonderful, beautiful, easy and genuine online video of the good plan. I'm such a lover of the operate! I might require a chew involving treat away any sapling any day.
Diablo 3 cd key



GW2 Gems

Melisa said...

Thanks for sharing this niche useful informative post to our knowledge, Actually SAP is ERP software that can be used in many companies for their day to day business activities it has great scope in future so do your sap training in Chennai
Regards,
sap course in Chennai|SAP Training in Chennai|SAP institutes in chennai|SAP BI Training In Chennai

Unknown said...

Well post, Thanks for sharing this to our vision. In recent day’s customer relationship play vital role to get good platform in business industry, Sales force crm tool helps you to maintain your customer relationship enhancement.
Regards,
Salesforce training in Chennai|Salesforce training center in Chennai|Salesforce training in velachery

Stephen said...

Mangaining customers and thereby increasing the company's revenue is the toughest thing to handle. But companies like Salesforce are dedicated to it by offering most useful and efficient tools. Your content is stating the same. Thanks for offering a worth able article to read. Keep up the activity of posting valuable posts.

Salesforce crm training in chennai | Salesforce admin training in chennai | Salesforce Training institutes in Chennai

Unknown said...

Data processing is the biggest challenge to veterans since last decade. With the invention of cloud solution the burden about data handling become zero percent(Cloud computing training in chennai). Thanks for providing such an useful content here. The information is explicit and clear(Cloud computing training institutes in chennai). Keep blogging like this.(Cloud computing training chennai)

Unknown said...
This comment has been removed by the author.
kimjhon said...

Well said! Many notable definitions about PHP Training in Chennai
PHP Training Chennai
PHP Course in Chennai

Unknown said...

Thanks for sharing this niche useful informative post to our knowledge.
brochure designers in chennai | brochure design company in chennai

rohini said...

The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
Selenium Training in Bangalore | Selenium Training in Bangalore | Selenium Training in Pune | Selenium online Training

manisha said...

Thanks for sharing such a great blog Keep posting..
PHP Training in Gurgaon
PHP Training institute in Gurgaon

Anonymous said...

Thank you for providing such an awesome article and it is very useful blog for others to read.
PHP Training in Delhi
PHP Training Course in Delhi