简体   繁体   中英

php include unterminated string literal message

The following works just fine:

var sFirstText=<?php include("first2.html"); ?>;

when first2.html looks like this INCLUDING the double quotes:

"<p>sentence one</p><p>sentence two</p>"

However, if first2.html looks like:

"<p>sentence one</p>
<p>sentence two</p>"

I get an unterminated string literal message. I hope to figure out how I can include the html without first having to remove the carriage return/line feed sequences.

Also, if I remove the double quotes and do:

var sFirstText="<?php include("first2.html"); ?>";

that won't work, returning a message I haven't yet been able to comprehend.

Basically I want to get simple html formatting into a field without having to remove the cr/lf sequences.

尝试在行末使用\\ ,以将字符串继续到下一行。

Javascript can not handle those newlines. You might wanna open the file, read contents and replace those with empty spaces instead.

Also, while getting the source content, you will need to escape the type of quote you're using to wrap it.

Here's what I would do.

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','"') ?>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM