473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

change using str_replace then parse php

ok - please forgive me if i dont make sence - ill try to explain what i want
to do

i have an file that i want to change a few srings in it (its a .htm file)

i use the str_replace funtion to replace the nessasary strings - i then want
to include the result in the php file that is doing the changes.

this is what i have got

MY index.php file is as per below
------------------
<?PHP
$file=implode("",file("main.html"));
$file=str_replace("$stringToFind","$NewString", $file);
echo $file;
?>
--------------------

but this doesnt parse any php that is in $file

i thought a way around this would be to save $file as say file.tmp and then
use an include statement to include this. then delete file.tmp

is there any other way around it

thanks


Jul 17 '05 #1
8 2145
i have worked it out - i used the output buffering .


"chris" <so*****@here.com> wrote in message
news:41********@funnel.arach.net.au...
ok - please forgive me if i dont make sence - ill try to explain what i
want to do

i have an file that i want to change a few srings in it (its a .htm file)

i use the str_replace funtion to replace the nessasary strings - i then
want to include the result in the php file that is doing the changes.

this is what i have got

MY index.php file is as per below
------------------
<?PHP
$file=implode("",file("main.html"));
$file=str_replace("$stringToFind","$NewString", $file);
echo $file;
?>
--------------------

but this doesnt parse any php that is in $file

i thought a way around this would be to save $file as say file.tmp and
then use an include statement to include this. then delete file.tmp

is there any other way around it

thanks


Jul 17 '05 #2
Use eval:

<?PHP
$file=implode("",file("main.html"));
$file=str_replace("$stringToFind","$NewString", $file);
eval( '?>' . $file . '<?php' );
?>
Hilarion
Jul 17 '05 #3
Hilarion wrote:
Use eval:

<?PHP
$file=implode("",file("main.html"));
$file=str_replace("$stringToFind","$NewString", $file);
eval( '?>' . $file . '<?php' );
?>
Hilarion


N.B. eval should be avoided at all costs it's "expensive"

Ref: http://uk2.php.net/manual/en/function.eval.php

~Cameron
Jul 17 '05 #4
Nothing "should be avoided at all costs" even if it's expensive. It only has
to be used wisely. (Speed and memory optimization is not the main goal
of every coding.)

Hilarion

PS.: My code should be: eval( '?' . '>' . $file . '<' . '?php ' );
Jul 17 '05 #5
Hilarion wrote:
Nothing "should be avoided at all costs" even if it's expensive. It
only has to be used wisely. (Speed and memory optimization is not the
main goal of every coding.)


In fact you should read "should only be used as a last resort". Fortunate,
the OP did find a better solution using output buffering.
JW

Jul 17 '05 #6
Hilarion wrote:
Nothing "should be avoided at all costs" even if it's expensive. It only has
to be used wisely. (Speed and memory optimization is not the main goal
of every coding.)

Hilarion

PS.: My code should be: eval( '?' . '>' . $file . '<' . '?php ' );


Well ok, I meant unless there is no other solution, just worded it
badly, and one of the comments on the function page contains the quote

"If eval() is the answer, you're almost certainly asking the
wrong question. -- Rasmus Lerdorf, BDFL of PHP"

~Cameron
Jul 17 '05 #7
d
It's also bad programming, in my experience. I've never seen or even heard
of a good reason for using it, where some extra coding couldn't do the job
much faster. I'm not criticising, just expressing my opinion :)

"Cameron" <ca*@foo.bar.invalid> wrote in message
news:co*******************@news.demon.co.uk...
Hilarion wrote:
Use eval:

<?PHP
$file=implode("",file("main.html"));
$file=str_replace("$stringToFind","$NewString", $file);
eval( '?>' . $file . '<?php' );
?>
Hilarion


N.B. eval should be avoided at all costs it's "expensive"

Ref: http://uk2.php.net/manual/en/function.eval.php

~Cameron

Jul 17 '05 #8
d
Exactly. Thanks Cameron & Rasmus :)

"Cameron" <ca*@foo.bar.invalid> wrote in message
news:co*******************@news.demon.co.uk...
Hilarion wrote:
Nothing "should be avoided at all costs" even if it's expensive. It only
has
to be used wisely. (Speed and memory optimization is not the main goal
of every coding.)

Hilarion

PS.: My code should be: eval( '?' . '>' . $file . '<' . '?php ' );


Well ok, I meant unless there is no other solution, just worded it badly,
and one of the comments on the function page contains the quote

"If eval() is the answer, you're almost certainly asking the
wrong question. -- Rasmus Lerdorf, BDFL of PHP"

~Cameron

Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fartsniff | last post by:
i found this code out in the ng, and its seems long and clunky, i am still experimenting with preg_match and _replace, but the syntax is a bit confusing. it seems i always misplace or mistype...
2
by: mrquake99 | last post by:
Hi, I am looking for php tools / scripts with which I can convert php generated HTML to PDF. I was looking at FPDF and HTML2PDF (based on FPDF). However, HTML2PDF can not handle nested lists and...
27
by: StevePBurgess | last post by:
With a string of authors such as: Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen, Shawyer, Andrea I would like to write a function to change every other comma into a semi...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
7
by: abscons | last post by:
Hi out there, I have an url that looks like this: http://host/file?var=value. I am trying to replace the "value" part in my url and assign it to a link on my page. I tried to parse the url but can...
3
by: robski | last post by:
Hello there, I am new to this forum and have done my best to look around for a solution to this problem. I am using PHP to generate an HTML Select drop down menu, with 4 options: function...
27
by: Jon Slaughter | last post by:
Can I modify code that I have included using <?php include("../Index.php"); ?> The Index.php file contains links that need to be modified to work. Index.php is basically an html file uses a...
5
by: Smiley | last post by:
I'm fooling around with using Eval and trying to manipulate a few things. I ran into a couple of weird results. First of all, in one place I used the following code: $filestring =...
1
by: Sean Kim | last post by:
From example in the book 'Programming PHP' (O'Reilly) (p.305) $theKey = 'DESTINATION'; $target = '{' . $theKey . '}'; $inValues = 'some.php'; //$theTemplate = str_replace("\{$theKey}",...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.