View Full Version : File contents - honour spaces

08-10-2012, 10:06 PM
I have saved my JS file as a text file. I use four spaces for the indenting and if I open the file in a text editor these spaces are intact. But when I use file_get_contents() print_r confirms that often these spaces are doubled-up to eight.

Has anyone encountered this and know of a solution? Here's my code for reference:

if (file_exists('includes/AndyG_js.txt')) {
$mylib = file_get_contents('includes/AndyG_js.txt');
} else {
$mylib = 'File not found, unfortunately! Contact the administrator of this site.';

echo <<< THECODE
<pre class="prettyprint"><code class="language-javascript">{$mylib}

08-10-2012, 10:21 PM
Scrub this question, sorry - they were tabs, not spaces :thumbsup:

However, it's a fairly large file - 1000 lines. What code would you use to load this in stages, or would you use something other than file_get_contents()?

08-10-2012, 11:23 PM
Loading in stages doesn't make any sense. If its a JS file, it would need it in its entirety to make any sense of it (assuming the JS needs the entire thing).
You can however read it in stages. Use fopen/fread/fclose instead.

08-10-2012, 11:35 PM
Thank you @Fou-Lu

I'm not using the JS itself, I'm outputting the text content (of the JS file) using Google's Prettify.

Actually, 1000 lines isn't that long. I suspect it's the prettify-ing that's slowing it down. I should try and create a simple timer..

Perhaps I could read the file length and get the contents and prettify it in two or three stages, still using get_file_contents. Maybe I could read just 1/10th of the file first and prettify it, so at least there's some content displaying whilst the rest is being parsed.

But I'm open to suggestions :thumbsup: