我使用fwrite将javascript添加到外部脚本。为了做到这一点,需要有一个带有java内部的php字符串。然而,“和'混淆了,我不知道如何解决它。这是代码:
$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); ?>
脚本执行我想要的操作,但是javascript中的语音标记与php字符串语音标记混合,如上所示,导致php抛出错误。有办法解决这个问题吗?
由于
答案 0 :(得分: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);
?>
答案 1 :(得分:1)
像这样在这里使用doc:
$page = <<<EOF
<script type="text/javascript">
.....enter the rest of the code here...
</script>
EOF;
echo $page;
查找手册以了解如何使用heredoc
答案 2 :(得分:1)
您有几种选择。
就个人而言,我会选择#3
答案 3 :(得分:1)
使用"''"
或'""'
或"\"\" "
来转义字符串。
答案 4 :(得分:0)
这是另一种方式,它是一种舒适和美丽的方式,你将保持概述,语法突出显示,而不必与引号斗争:
<?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);
这会将你的js写入文件