views:

188

answers:

7

I currently am trying to echo the contents of a text file within JavaScript. Everything works but there is a problem. The text within the text contains apostrophes, which is throwing everything off. Below is a portion of the code that I am using. I am using this code for MANY text files. I have considered changing each apostrophe to "\'" in each text file, but there are a lot of text files to deal with. I was just curious if there was another way to work around this. Any ideas are greatly appreciated.

<?php
     $lyrics = file_get_contents('Includes/Songs/Lose_My_Mind.txt');
?>

JavaScript snippet:

var scrollercontent='<?php echo $lyrics; ?>'
+2  A: 

Try to use addslashes() function. In your case:

var scrollercontent='<?php echo addslashes($lyrics); ?>'
Alex
Ok I tried this and <?php echo addslashes($lyrics); ?> works by itself, but when used within the JavaScript it doesn't?
Nate Shoffner
Ok, I think I figured out the problem, the text within the text files are not one continuous line, but rather a bunch of lines. Is there a way to make it automatically recognize new lines? I've tried putting "\n" at the end of each line in one of the text files and it doesn't work. :(
Nate Shoffner
Use `json_encode` if available.
Gumbo
A: 

Try changing this

var scrollercontent='<?php echo $lyrics; ?>'

to this

var scrollercontent='<?php echo addslashes($lyrics); ?>'

or

var scrollercontent='<?php echo htmlentities($lyrics); ?>'

these should help escape or entitize quotes etc...

Chris Gutierrez
A: 

Have you tried:

<?php $lyrics = addslashes(file_get_contents('Includes/Songs/Lose_My_Mind.txt')); ?>

Christopher Altman
+2  A: 

Whilst addslashes will mostly work, this is better:

var scrollercontent= <?php echo json_encode($lyrics, JSON_HEX_TAG); ?>;

json_encode works for any datatype, not just strings. For strings it adds the quotes at the sides for you, and it will work for any character including control codes such as the newline character.

The JSON_HEXs are a PHP 5.3 feature. HEX_TAG replaces < with a JavaScript string literal encoding like \x3C, which means you can put any string literal in a <script> block without worrying that it might contain a </script> sequence that would prematurely end the script block. (Even just </ on its own is technically invalid.)

bobince
That works great! Just one thing, the text within the text files are not one continuous line, but rather a bunch of lines. Is there a way to make it automatically recognize new lines? I've tried putting "\n" at the end of each line in one of the text files and it doesn't work. The contents of scrollercontent are automatically scrolled using JavaScript and I would like to keep it in the same format.
Nate Shoffner
Any newlines in the PHP string will certainly be passed through to JavaScript. Maybe it's how they're being displayed that's the problem? A newline in HTML source will just display as a space. You'd need to create a `<br>` element for each newline, or wrap each line of text in its own `<div>`, to display the newlines as actual new lines.
bobince
+1  A: 

Try

addslashes(nl2br($lyrics))

(nl2br replaces new lines with <br> tags.)

jnunn
This fixes the newline problem perfectly! Thanks.
Nate Shoffner
A: 

You could add the following line after your file_get_contents command:

$lyrics = str_replace("\'","\\'",$lyrics);

This will change all of the single apostrophes to escaped apostrophes and should play nice with Javascript.

Carlson Technology
A: 

<?php $lyrics = nl2br(htmlentities(file_get_contents('Includes/Songs/Lose_My_Mind.txt'), ENT_QUOTES)); ?>

var scrollercontent="<?php echo $lyrics; ?>";

Julio