0

I am using fwrite to add javascript to a external script. In order to do this there needs to be a php string with the java inside. However the " and ' get mixed up and I am not sure how to fix it. Here Is the code:

$page = 
"<script type="text/javascript"> 
function outf(text) { 
 var mypre = document.getElementById("output"); 
 mypre.innerHTML = mypre.innerHTML + text; 
} 
function builtinRead(x) {
 if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
 throw "File not found: '" + x + "'";
 return Sk.builtinFiles["files"][x];
}
function runit() { 
 var prog = document.getElementById("yourcode").value; 
 var mypre = document.getElementById("output"); 
 mypre.innerHTML = ''; 
 Sk.canvas = "mycanvas";
 Sk.pre = "output";
 Sk.configure({output:outf, read:builtinRead}); 
try {
 eval(Sk.importMainWithBody("<stdin>",false,prog)); 
}
catch(e) {
 alert(e.toString())
}
} 
</script> 
<?php
fwrite($fh, $page); ?>

The Script does what I want it to do, however the speech marks within the javascript mix with the php string speech marks as you can see above, causing the php to throw an error. Is there a way to fix this?

Thanks

asked Feb 19, 2015 at 10:06
1
  • 1
    Escape your "s like this: \", or if you think that is dirty you can replace your "s with 's in your JS code. Commented Feb 19, 2015 at 10:07

5 Answers 5

1
$page = 
"<script type=\"text/javascript\"> 
function outf(text) { 
 var mypre = document.getElementById(\"output\"); 
 mypre.innerHTML = mypre.innerHTML + text; 
} 
function builtinRead(x) {
 if (Sk.builtinFiles === undefined || Sk.builtinFiles[\"files\"][x] === undefined)
 throw \"File not found: '\" + x + \"'\";
 return Sk.builtinFiles[\"files\"][x];
}
function runit() { 
 var prog = document.getElementById(\"yourcode\").value; 
 var mypre = document.getElementById(\"output\"); 
 mypre.innerHTML = ''; 
 Sk.canvas = \"mycanvas\";
 Sk.pre = \"output\";
 Sk.configure({output:outf, read:builtinRead}); 
try {
 eval(Sk.importMainWithBody(\"<stdin>\",false,prog)); 
}
catch(e) {
 alert(e.toString())
}
} 
</script>";
<?php
fwrite($fh, $page); 
?>
answered Feb 19, 2015 at 10:12
Sign up to request clarification or add additional context in comments.

Comments

1

use here doc like so:

$page = <<<EOF
 <script type="text/javascript"> 
 .....enter the rest of the code here...
 </script>
EOF;
echo $page;

find a manual to learn how to use heredoc

answered Feb 19, 2015 at 10:13

Comments

1

You have several options.

  1. Replace all the double quotes in your javascript with single quotes
  2. Escape all the quotes as Andreas mentioned
  3. Use heredoc

Personally, I would go for #3

answered Feb 19, 2015 at 10:14

Comments

1

Use "''", or '""', or "\"\" " to escape the strings.

meskobalazs
16.1k2 gold badges44 silver badges63 bronze badges
answered Feb 19, 2015 at 10:18

Comments

0

This is another way which is some kind comfortable and beautiful for you will keep overview, syntax highlighting and don't have to fight with the quotes:

<?php
ob_start(); // start output buffering
?> <!-- mark that there is no more php following, but HTML -->
<!-- copy your js here (without the very first quotes) -->
<script type="text/javascript"> 
function outf(text) { 
 var mypre = document.getElementById("output"); 
 mypre.innerHTML = mypre.innerHTML + text; 
} 
// ............
</script>
<?php
$script = ob_get_contents();
ob_end_clean();
fwrite($fh, $script);

this will write your js to the file

answered Feb 19, 2015 at 10:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.