473,811 Members | 2,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read *parsed* PHP file into a variable?

Hello,

I have this strange including problem:

I want to read a piece of HTML, residing in some file, into a
variable, not echo it out. So far so easy - but now the HTML contains
a line of PHP. And I wish not to get the PHP code into my variable but
the parsed result, just like if the variable was a client ;-). I wish
to keep the nice ?> ...<? HTML area (with highlighting in my editor),
and therefore include etc. don't help because they would echo it out.
heredoc also doesnt help (if this may come up to your mind).

Is there anything I can do...?!! The problem sounds so simple... the
more for such a great tool like PHP ... but... ?!!

Thanx a lot,
Daniel

PS Please post also if you know for sure that there is NO solution to
my problem

Aug 24 '05 #1
6 15372
"Daniel Loose" wrote:
Hello,

I have this strange including problem:

I want to read a piece of HTML, residing in some file, into a
variable, not echo it out. So far so easy - but now the HTML contains
a line of PHP. And I wish not to get the PHP code into my variable but
the parsed result, just like if the variable was a client ;-). I wish
to keep the nice ?> ...<? HTML area (with highlighting in my editor),
and therefore include etc. don't help because they would echo it out.
heredoc also doesnt help (if this may come up to your mind).

Is there anything I can do...?!! The problem sounds so simple... the
more for such a great tool like PHP ... but... ?!!


Easy: $html = file_get_conten ts('http://www.example.com/foo.bar');

Take a look here: <http://php.net/file_get_conten ts>

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/
Aug 24 '05 #2
"Daniel Loose" <no*****@web.de > kirjoitti
viestissä:43*** ***********@new s.cs.tu-berlin.de...
Hello,

I have this strange including problem:

I want to read a piece of HTML, residing in some file, into a
variable, not echo it out. So far so easy - but now the HTML contains
a line of PHP. And I wish not to get the PHP code into my variable but
the parsed result, just like if the variable was a client ;-). I wish
to keep the nice ?> ...<? HTML area (with highlighting in my editor),
and therefore include etc. don't help because they would echo it out.
heredoc also doesnt help (if this may come up to your mind).

Is there anything I can do...?!! The problem sounds so simple... the
more for such a great tool like PHP ... but... ?!!

Sounds like you could use buffering.
<?php

ob_start(); // Start output buffering.
// Instead of outputting anything, the output is now stored in a buffer.
include('yourfi le.php');
// There, now the file has been included and php parsed.
// It isn't output, it goes into the buffer.
$yourfile = ob_get_clean();
// Now we read everything that has been buffered... And stops buffering.

?>

Result: the file you included has been parsed by the php engine and stored
inside $yourfil, and nothing was output. Sounds like goal achieved.

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.ber keley.edu/>
Kimmo Laine <et************ ****@5P4Mgmail. com>
Aug 24 '05 #3
Just discovered http://de.php.net/eval and on that url especially the
user contributed function eval_html3(). But can't get it to work.

I made a simple test page:
http://mueller.zems.tu-berlin.de/~loose/test/test.php
Perhaps you feel like helping to fix the function. (Or to tell me what
I have misunderstood.) I just renamed eval_html3 to eval_html and
replaced <?php by <?. It's now supposed to work but it doesnt. Here
the test files:

test.php:
------------
$s = eval_html(file_ get_contents('. js.php', 0));
$t = eval_html(file_ get_contents('. css.php', 0));

function eval_html($stri ng) {
$string = '<? ?>'.$string.'< ? ?>';
$string = str_replace( '?>', '', str_replace( array( '<?', '<?' ),
'', preg_replace_ca llback( "/\?>(.*?)(<\?|<\ ?)/", "my_eval", $string )
) );
return eval($string);
}
function my_eval($arr) {
return ('echo stripslashes("' .addslashes($ar r[0]).'");');
}

..js.php and .css.php:
------------------------------
contain plain js/ css plus one line
<? $a = 6+7; echo ".testClass { font-size: ".$a."px; }\n\n"; ?>
or resp.
<? $a = 4+5; echo 'testVar = '.$a;; ?>
No errors when called directly.

test.php itself does nothing (blank screen), I only want to get away
the errors. You may also download this small test if you want to
figure it out on your own system (See Link)

Thanx a lot! , Daniel

PS Thx to the other poster, will check your idea out soon as possible.
Aug 24 '05 #4
Daniel Loose wrote:
Just discovered http://de.php.net/eval and on that url especially the
user contributed function eval_html3(). But can't get it to work.


Better have a look at Kimmo's suggestion, because that's the way to go...
JW

Aug 24 '05 #5
Sounds like you could use buffering.
<?php

ob_start(); // Start output buffering.
// Instead of outputting anything, the output is now stored in a buffer.
include('yourf ile.php');
// There, now the file has been included and php parsed.
// It isn't output, it goes into the buffer.
$yourfile = ob_get_clean();
// Now we read everything that has been buffered... And stops buffering.


Pretty cool, thanx so much! Never heard of output buffering before.
Yeah...

Guess no need to fight with my other Reply anymore (concerning
eval_html()).

Enjoy, D.
Aug 24 '05 #6
Daniel Loose wrote:
Hello,

I have this strange including problem:

I want to read a piece of HTML, residing in some file, into a
variable, not echo it out. So far so easy - but now the HTML contains
a line of PHP. And I wish not to get the PHP code into my variable but
the parsed result, just like if the variable was a client ;-). I wish
to keep the nice ?> ...<? HTML area (with highlighting in my editor),
and therefore include etc. don't help because they would echo it out.
heredoc also doesnt help (if this may come up to your mind).

<snip>

Sounds like you're going to use/eval users' input?--if so, it's a
*serious* security issue. Use output buffering with runkit
<http://in.php.net/runkit>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

Aug 25 '05 #7

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

Similar topics

6
7757
by: chuck amadi | last post by:
Hi , Im trying to parse a specific users mailbox (testwwws) and output the body of the messages to a file ,that file will then be loaded into a PostGresql DB at some point . I have read the email posts and been advised to use the email Module and mailbox Module. The blurb from a memeber of this list . Im not at work at the moment So I cant test this out , but if someone could take a look and check that im on the write track as this...
0
3624
by: Stuart Miller | last post by:
When using Java to read in an XML file, I am having problems getting the XML file to be parsed against the specified DTD. Background: I have an existing Java application that allows users to modify values in a database. To improve performance - we're looking to enable multiple record modification by using XML files. The user downloads the required
6
3480
by: Steve | last post by:
Hi, I'm trying to convert a file reading loop into one using streams. The BSD OS read API returns the number of bytes read, but istream::read returns itself. How can I find out the number of bytes actually read? What the code fragment should do is read up to 1000 bytes into a buffer, or finish early if reading failed. Just your average read loop. I have: (this is a simplified version; I know there's no detailed error
3
4323
by: Rakesh Sinha | last post by:
I have a very trivial question. But I searched in google / archives of this group to get the answer, checked the C++ FAQ - but did not precisely what I was looking for. The problem is with respect to reading 'long double's from a stream separated by comma. #include <iostream>
2
3145
by: ad | last post by:
I have a xml file, say c:\book.xml; How can I read the xml in that file to a string variable?
2
1129
by: PJ6 | last post by:
OK I know I can just open up the CSS file directly and use a text reader if I need to, but I'm sure there is a built-in way to do this, especially to get the information already parsed... I just can't find it. Can somone point me in the right direction? TIA, Paul
5
2263
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write and then read: string1, integer1, double1
6
6012
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to assign a value to them. I wanted to trap for this exception in a subclass using super but it doesn't happen. I have read Guido's tutorial on new style classes and Shalabh's tuturial on new style attributes and methods, and thought I understood...
1
2176
by: jim.omalley | last post by:
I'm trying to write an interface to send scoreboard data from an XML file generated by a football stats program to a Chyron CODi character generator connected to my COM1 serial port (all operations on same machine). I'm already able to send static commands written into HTTP forms to control the CG via serial COM1. What I'm needing is a way to read and parse variables from the XML, then generate a CODi command string which can be written...
0
9724
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10394
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9201
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.