include a .txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
polishvalor
Forum Newbie
Posts: 13
Joined: Sat Nov 06, 2010 10:45 pm

include a .txt file

Post by polishvalor »

I use include for a text file forgetting how to make sure " and ' are shown correctly. Can someone please remind me?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: include a .txt file

Post by danwguy »

if your text file is outputting those to the screen put a \ infront of them or use " instead of " in your txt file.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: include a .txt file

Post by John Cartwright »

You generally only need to apply this when your text is inserted into html elements, where you need to be careful not to break out of the element by applying a quote. But to answer your question:

Code: Select all

htmlspecialchars($yourstring, ENT_QUOTES);
polishvalor
Forum Newbie
Posts: 13
Joined: Sat Nov 06, 2010 10:45 pm

Re: include a .txt file

Post by polishvalor »

Code: Select all

htmlspecialchars($yourstring, ENT_QUOTES);
[/quote]

and how would this look like with an include()?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: include a .txt file

Post by John Cartwright »

polishvalor wrote:

Code: Select all

htmlspecialchars($yourstring, ENT_QUOTES);
and how would this look like with an include()?[/quote]

Why are you using an include()? Why not use a function that will return the string instead. I.e., file_get_contents(), or file(), or fopen() + fgets() + fclose()

If you are insistent on using an include(), then you need to out output buffering to capture the output. I.e.,

Code: Select all

ob_start();
include($yourTextFile);
$ret = ob_get_contents();
ob_end_clean();
Post Reply