I have a text file that i would like to display a part of so i use this code:

$page = file_get_contents('news.txt');
echo $page;

But in news.txt if i have this code <?php echo"hello world!"; ?> It will not display... when i look at the source code of my page i see my code in red. How can i display it.

btw: I know about include but thats not what i need.

Thanks for your help, Sanchixx.

Recommended Answers

All 17 Replies

change line 2 to $page.
Change the news.txt to hello world! with nothing else around it.

What do you want as a result "hello world" or "<?php echo "hello world!"?>"?

That was not in my original code, it's a typo. The code works but when i want to display php it doesen't display it. And this is not the code i use i did not want to display a long pagination script so i posted only that.

$page = highlight_file("news.txt", true);
echo $page;

I want to compile the code in my text file. I'm able to compile php file. But the code in news.txt does not wan't to compile. I was not clear before.

include "news.txt" or required "news.txt" didn't help?

If the file that is being 'got' by file_get_contents() has the extension '.txt' will the server parse the file as PHP?
Change '.txt' to '.php' and see if it helps.
If you want to keep the extension as .txt, and still have the file parsed by the server, you could use the include() directive.

I tried that it did not work :'(

PHP scripts are not compiled, they are interpreted (parsed) by the webserver on the fly as and when the script is called by the broswer.
If you want the PHP in the text file to be parsed (what you would call compiled), then you can use - include() or require() and the extension of the file is irrelevant.
It seems that none of the filesystem functions will parse the PHP code in the file whatever the extension is (.php or .txt)

But i would only like to show a part of the file i seperated paarts of my file by a character, then i explode it in an array so i can paginate it: is possible to include a element of an array?

Can't you just use two different files? Is there a reason that two types of data (plain text and PHP code) must be in the same file? From your questions I think you have gone off track and need to think about this again. What exactly do you want to acheive?

Member Avatar for diafol

If the file is php code and you've placed parts of it into an array, I fail to see how you're going to run that code unless you use the eval() function - which you should not. Tempting... no... under no circumstances should you even consider it... well, maybe... no eval is evil!

Thank you i will be able to solve my problem :)

Member Avatar for diafol

You've asked this question and posted almost no code. I can't understand what you're trying to do. Any chance that you can post more code?

I've solved my problem thanks to you but what are the dangers of eval?

Member Avatar for diafol

If user input can reach the eval() function, you could be in trouble.

A DW user once told us that he had this eval() function on one of his pages. So, I typed in some code to get a list of all the files in his webroot and I then pasted my name all over his frontpage. I then posted him a PM with his DB connection details. He got the message and took it off. :)

If you can isolate / secure any input to the function, you may be able to use it safely. But this, as the manual says, really should be a last resort.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.