473,597 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.p hp#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 1785
Rik
On Tue, 24 Jul 2007 19:59:27 +0200, Sander Tekelenburg
<us**@domain.in validwrote:
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.p hp#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('f ile.php').'<?ph p');
$string = ob_get_clean();
Then again, that would basically be the same as:
ob_start();
include('file.p hp');
$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.p hp');
//write output to temporary file
fwrite($fh,ob_g et_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.tvzecvwtqnv 3q9@metallium>,
Rik <lu************ @hotmail.comwro te:
On Tue, 24 Jul 2007 19:59:27 +0200, Sander Tekelenburg
<us**@domain.in validwrote:
[...] 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('f ile.php').'<?ph p');
$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.p hp');
$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.cla ss.php. See
<http://www.bitfolge.de/rsscreator-en.html>

So the code essentially is this:

require_once("f eedcreator.clas s.php");
$news = new UniversalFeedCr eator();
//loop through list of news files
ob_start();
include($s_path tofile);
$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
8907
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: MyExpression = "DateTime.Today.Month % 2) = 0?"Even\":\"Odd\""; string strResultString = MyExpression.Evaluate();
1
3091
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 context.nextNode().selectNodes(expression);
0
5039
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 what is causing this. Any Help would be great!!! Event Type: Warning Event Source: W3SVC Event Category: None Event ID: 1012
135
7430
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 which is better. It has become what's known as “religious war” — a heated fight over trivia. In this essay, i like to explain what is the situation behind it, and which is proper.
1
1051
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 code , I'd like to test that docserver.domain.net is currently serving pages .... (before including URLs to this server in links on a page that I'm constructing.
1
1889
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 the following code to do the same thing: nodes = document.evaluate("/html/body/table/tbody/tr/td/table/tbody/tr/td/div/ul/li", document, null,XPathResult. ORDERED_NODE_SNAPSHOT_TYPE , null);
15
2246
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) web server, so this is not a cross-site scripting attack. But I would prefer not to taint the target page with any extra logic to do this. (I will if I must.) The calling page has these elements:
16
4599
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I protect my javascript code? ----------------------------------------------------------------------- With clientside Javascript you can't as your code is distributed in source form and is easily readable. With JScript, there is the Script Encoder (see MSDN), but this is nothing more than obfuscation. Disabling the right mouse button also does...
2
12286
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) end sub Well, of course it's posible, and can be done with some characters mannaging. This way you can complicate it as much as you want. The way i usualy do it is in 5 simple steps: 1. Create 2 string arrays, and save numbers in one and...
0
7884
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8267
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
8380
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
8024
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
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5423
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
3921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2394
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1229
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.