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.
$string = "
Hello ?> . You are wellcome !";
echo $string;
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:
$string = "
echo $username; ";
eval ( $string ) ;
This is the same thing than:
echo $username;
If you have a string that is a html with some php tags, like the first example, do the following:

$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);


Hello SMITH . You are wellcome !


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.
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
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.
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…
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...
This comment has been removed by the author.
caroline jesi said...

These all are notable facts… I stick with responsive website design for my business. With the mobile marketing is at its peak stage, having website that goes comfy on all devices will help your business. You can check my website for more details on the importance of responsive website design. PHP Training Institute in Chennai | Best PHP training in Chennai | PHP Training Institute in Chennai

kim john said...

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

ASO Services said...

Good post and I like it very much. By the way, anybody try this increase app downloads? I do not how to use.

JamesWilliam 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...
PHP developers in chennai | PHP developers in India

JamesWilliam said...

Interesting post! This is really helpful for me. I like it! Thanks for sharing!
Mobile application developers in Chennai | PHP developers Chennai

JamesWilliam said...

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

Rohini .R 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