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 !


12 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

fifa ut coins said...

such a wonderful discover -- cannot wait around to determine that which you 2 develop!



fifa ut coins
league of legends elo boost
fifa coins online
cheapest fifa 14 coins

Roshini RS said...

Great Post, Actually PHP is a beautiful source for developing a database driven web application, I love this post, thanks for spending your time for discussing about this topic.
Regards,
PHP Training in Chennai

Fully Help said...

Nice blog...Very useful information is providing by ur blog. Great beginning php tutorials Very clear and helpful for beginners.

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

jhansi joe 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

Andrew Son 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)

jhansi joe said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
Regards,
cognos Training in Chennai|Best COGNOS Training Institute in Chennai|cognos Certification

mary Brown said...

I have read your blog its very attractive and impressive. I like it your blog.

PHP Training in Chennai | Online PHP Training

PHP Training in Chennai | Online PHP Training

Iqbal Dlk said...

very nice and informative blog
dot net training in chennai
java training in chennai

Atul Srivastava said...

Hey Nice blog,Thanks for sharing this blog.!!!

Best Java Summer training in lucknow

Best Php Summer training

Summer training

Best Summer training Company

Job Oriented training in Lucknow