以下工作正常:
var sFirstText=<?php include("first2.html"); ?>;
当 first2 时.html看起来像这样,包括双引号:
"<p>sentence one</p><p>sentence two</p>"
但是,如果 first2.html如下所示:
"<p>sentence one</p>
<p>sentence two</p>"
我收到一条未终止的字符串文字消息。我希望弄清楚如何在不首先删除回车/换行序列的情况下包含 html。
另外,如果我删除双引号并执行以下操作:
var sFirstText="<?php include("first2.html"); ?>";
这行不通,返回一条我还无法理解的消息。
基本上,我想将简单的html格式格式化到一个字段中,而不必删除cr/lf序列。
尝试在行尾使用 '
将字符串继续到下一行。
Javascript无法处理这些换行符。您可能希望打开文件,读取内容并用空格替换它们。
此外,在获取源内容时,您需要转义用于包装它的引用类型。
这就是我要做的。
function contents($file, $wrappingQuote='''') {
$fh = fopen($file,'r');
$contents = fread($fh,filesize($file));
fclose($fh);
echo preg_replace('~''~',''''.$wrappingQuote,preg_replace('~'r?'n~','',$contents));
}
(...
var str = '<?php contents('first2.html') ?>';
// or
var str = "<?php contents('first2.html','"') ?>";