473,385 Members | 1,523 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,385 software developers and data experts.

evaluate code before serving it

Situation: I store news articles as individual PHP files. Each file
contains HTML and now and then some embedded PHP snippets.

Serving those news articles on the Web works fine, through include().
But I want to also serve them through RSS, and found that in that case
the PHP source code is served -- it's not being evaluated.

So it seems obvious that I need to first evaluate those PHP snippets,
store the result in a variable, and use *that* as the input for the RSS
generator script. I thought that'd be pretty straight-forward, but for
the life of me I can't figure out how to do it.

I've experimented with eval() and ob_start() but either I misunderstand
how to use them, or they're not the right tools for this use.

<http://de.php.net/manual/en/function.eval.php#76339claims a solution,
but it doesn't work for me. (I get no output whatsover using that
approach.)
Any suggestions? TIA!

--
Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>

Mac user: "Macs only have 40 viruses, tops!"
PC user: "SEE! Not even the virus writers support Macs!"
Jul 24 '07 #1
3 1773
Rik
On Tue, 24 Jul 2007 19:59:27 +0200, Sander Tekelenburg
<us**@domain.invalidwrote:
Situation: I store news articles as individual PHP files. Each file
contains HTML and now and then some embedded PHP snippets.

Serving those news articles on the Web works fine, through include().
But I want to also serve them through RSS, and found that in that case
the PHP source code is served -- it's not being evaluated.

So it seems obvious that I need to first evaluate those PHP snippets,
store the result in a variable, and use *that* as the input for the RSS
generator script. I thought that'd be pretty straight-forward, but for
the life of me I can't figure out how to do it.
Depends on how you generator script works offcourse... Normally is
shouldn't be a problem.
I've experimented with eval() and ob_start() but either I misunderstand
how to use them, or they're not the right tools for this use.

<http://de.php.net/manual/en/function.eval.php#76339claims a solution,
but it doesn't work for me. (I get no output whatsover using that
approach.)
Simple eval solution:
The manual says: "To mix HTML output and PHP code you can use a closing
PHP tag to leave PHP mode."

ob_start();
eval('?>'.file_get_contents('file.php').'<?php');
$string = ob_get_clean();
Then again, that would basically be the same as:
ob_start();
include('file.php');
$string = ob_get_clean();

Which is preferable.

It could be that your RSS-generator only swallows files/urls, and not
strings? In that case:

//get a unique filename in temporary dir
$file = tempnam();
//open file
$fh = fopen($file,'w');
//start buffer
ob_start();
//run file
include('file.php');
//write output to temporary file
fwrite($fh,ob_get_clean());
//...And serve the filename $file to your script.
//remove temporary file
unlink($file);

You might get into trouble when the script sets headers though...

More simple solution: if the RSS generator gets urls, just give him the
exact url of the file holding the article.

As you can see, without knowing anything about your RSS generator we're
flying blind here.
--
Rik Wasmus
Jul 24 '07 #2
Sander Tekelenburg wrote:
So it seems obvious that I need to first evaluate those PHP snippets,
store the result in a variable, and use *that* as the input for the RSS
generator script.
Post your RSS generator script.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 33 days, 21:55.]

Parsing an HTML Table with PEAR's XML_HTTPSax3
http://tobyinkster.co.uk/blog/2007/0...table-parsing/
Jul 24 '07 #3
In article <op.tvzecvwtqnv3q9@metallium>,
Rik <lu************@hotmail.comwrote:
On Tue, 24 Jul 2007 19:59:27 +0200, Sander Tekelenburg
<us**@domain.invalidwrote:
[...] Each file
contains HTML and now and then some embedded PHP snippets.

[...] I need to first evaluate those PHP snippets,
store the result in a variable, and use *that* as the input for the RSS
generator script.
[...]
ob_start();
eval('?>'.file_get_contents('file.php').'<?php');
$string = ob_get_clean();
Yeah, I had tried that. But for some reason this results in $string
being (defined but) empty.
Then again, that would basically be the same as:
ob_start();
include('file.php');
$string = ob_get_clean();
Bingo! That works. Thanks!

[...]
It could be that your RSS-generator only swallows files/urls, and not
strings?
It eats strings. I'm making use of FeedCreator.class.php. See
<http://www.bitfolge.de/rsscreator-en.html>

So the code essentially is this:

require_once("feedcreator.class.php");
$news = new UniversalFeedCreator();
//loop through list of news files
ob_start();
include($s_pathtofile);
$s_content = ob_get_clean();
$item = new FeedItem();
$item->description = $s_content;
$item->addItem($item);
//end loop
$news->saveFeed("RSS1.0", "path/file.xml", false);
In that case:
[...]
//write output to temporary file [...]
Yeah, I was considering going there. Glad I don't have to :)

--
Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>

Mac user: "Macs only have 40 viruses, tops!"
PC user: "SEE! Not even the virus writers support Macs!"
Jul 24 '07 #4

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

Similar topics

2
by: John Spiegel | last post by:
Hi all, Is it possible to have an uncompiled C# expression evaluated at runtime? I'd like to store an expression within an XML file then evaluate it when the time comes, something like: ...
1
by: David Laub | last post by:
I have no problems running the following dynamic XPath evaluator form MSXSL: <msxsl:script implements-prefix="dyn" language="jscript"> evaluate(context, expression) { return...
0
by: jack | last post by:
I am getting this error on my web server runnint ASP.NET on Windows 2003, it kills the serving web pages. The only way to get the web pages to start serving is to restart the server. I can't find...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
1
by: John A Grandy | last post by:
How to write code to test that a web server is currently serving pages ? For example, in a config file I have : <DocumentServer>http://docserver.domain.net/docs/en-us/doc</DocumentServer> In...
1
by: shellon | last post by:
Hi all: when I use XPather(a firefox extension) to evaluate the expression: "/html/body/table/tbody/tr/td/table/tbody/tr/td/div/ul/li" it tells me there are 7 matching Nodes. but when I use...
15
by: Phlip | last post by:
Javascripters: I have an outer page and an inner iframe. The outer page calculates some javascript, and wants the inner frame to run it. The inner frame should hit a page on the same (private)...
16
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I protect my javascript code? ----------------------------------------------------------------------- ...
2
kadghar
by: kadghar | last post by:
Many people asks if there is a way to write a mathematical expression, writen as a string in a text box, so they can do something like: sub something_click() textbox2.text=eval(textbox1.text)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.