dongyi9298 2014-01-17 19:21
浏览 176
已采纳

使用PHP从通用文件构造JavaScript代码时出现“SyntaxError:unterminated string literal”

I'm trying to use PHP to generate a JavaScript switch block by taking input from TemplateList.txt. It works fine when I only have one line in TemplateList.txt, but when I have two or more I get an error in the console saying I have a

SyntaxError: unterminated string literal

on the line of the php opening tag, I can't see any unclosed strings.

<?php 
$listHandle = fopen('templates/TemplateList.txt', 'r');

while (!feof($listHandle)) {                    
   $thisTemplate = fgets($listHandle);   
   echo "case \"" . $thisTemplate . "\": " . $thisTemplate . "();  break; "; 
}  

fclose($listHandle);
?>

My TemplateList.txt file looks like this:

heart_by_john
dog_by_sue

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • dongtu0088 2014-01-17 20:49
    关注

    The fgets() function includes the line break in the returned string, so you add a line break to your JS inside the string. Replace

    $thisTemplate = fgets($listHandle);
    

    by

    $thisTemplate = trim(fgets($listHandle));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波