1

I'm using PHP to create a JavaScript document. It does do two things:

  1. Read a directory containing some HTML files that I use as templates and then output an object containing key: value pairs that represent the filename: content, which will end up similar to this:

    var HTML = {
        "blogpost.html": '<div>{post}</div>',
        "comment.html" : '<div class="comment">{comment}</div>'
    };
    

    Which allows me to use HTML["template.html"] to append templated data that I receive from AJAX requests.

  2. Read a directory containing JavaScript files and output the content of those straight into the document.

Locally it's working fine, but I've been getting this error when I try it once uploaded:

Uncaught SyntaxError: Unexpected token ILLEGAL

I've tried wrapping the output I get from each of the HTML and JS files in things like:

preg_replace('/\s{2,}/', '', $output);
addslashes($output);
mysql_real_escape_string($output);

And a combination of those, but still the same error.

How can I reliably output the HTML and JavaScript I'm trying to place in the output?

Here's the current entire PHP script I am using (which works locally but not online weirdly):

header("Content-type: application/x-javascript");


// Write HTML templates.
$dir = dir($_SERVER['DOCUMENT_ROOT'] . '/view/html/');
$files = array();

while($file = $dir->read())
{
    if(strpos($file, ".html"))
    {
        $key = substr($file, 0, strpos($file, ".html"));
        array_push($files, '"' . $key . '": \'' . compress(file_get_contents($dir->path . $file)) . "'");
    }
}

echo 'var HTML = {' . implode(",", $files) . '};';




// Output other JavaScript files.
$js = array();

array_push($js, file_get_contents("plugin/jquery.js"));
array_push($js, file_get_contents("plugin/imagesloaded.js"));
array_push($js, file_get_contents("plugin/masonry.js"));
array_push($js, file_get_contents("base/master.js"));
array_push($js, file_get_contents("plugin/ga.js"));

echo implode("", $js);


// Compress a JavaScript file.
function compress($str)
{
    return addslashes(preg_replace('/\s{2,}/', '', $str));
}
1
  • have you looked for what character in your resultant js file (where is an example) is responsible for the error? Have you passed it through jshint or similar? Chrome's pretty-print function (the "{}" button) is pretty handy for finding things like that if you want to do it in the browser. Commented Sep 25, 2012 at 6:36

4 Answers 4

2

You can use json_encode() for any PHP -> JS conversion:

while ($file = $dir->read()) {
    if(strpos($file, ".html")) {
        $key = substr($file, 0, strpos($file, ".html"));
        $files[$key] = compress(file_get_contents($dir->path . $file));
    }
}

echo 'var HTML = ' . json_encode($files) .';';
Sign up to request clarification or add additional context in comments.

Comments

1

That's a parser error, so the problem happens before your code is even run.

I recommend checking the PHP versions of the two runtimes you're using. It would be ideal to develop and test with the same runtime that you plan to deploy to.

Comments

0

This happened to me before as well.

I'm assuming you copied part of the code you posted on a website like Github, or maybe your editor has stuffed up.

Invisible characters have been known to lurk in such documents.

A fix to this error is type the line of code with the error, the line above it, and the line underneath it in a fully plain-text editor like Notepad (Windows) or TextEdit (Mac). After typing it in, use Ctrl-A or Cmd-A (select all), then copy it and replace the code in your normal code editor.

Should fix the error.

Comments

0

I've worked out how to solve the problem in my current situation.

Background:

My local machine is running PHP Version 5.3.5
My host is running PHP Version 5.2.17

The problem was occurring at the end of each of the loaded HTML documents, where there wasn't a space or tab on the last line of the document, e.g.

<div>
    content
</div> <-- at the beginning of this line

Solution:

I changed the preg_replace() statement that was working with the output of each file so that it would also match newlines, which seems to have fixed it.

return preg_replace('/\s{2,}/', '', $str); // Old
return preg_replace('/\s{2,}|\n/', '', $str); // New

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.