Page 1 of 1

include a .txt file

Posted: Fri Jan 21, 2011 2:22 pm
by polishvalor
I use include for a text file forgetting how to make sure " and ' are shown correctly. Can someone please remind me?

Re: include a .txt file

Posted: Fri Jan 21, 2011 2:25 pm
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.

Re: include a .txt file

Posted: Fri Jan 21, 2011 2:28 pm
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);

Re: include a .txt file

Posted: Fri Jan 21, 2011 3:10 pm
by polishvalor

Code: Select all

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

and how would this look like with an include()?

Re: include a .txt file

Posted: Fri Jan 21, 2011 3:13 pm
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();