为什么我'm得到错误:“;SyntaxError:意外的标记<&”;

Why i'm getting error: "SyntaxError: Unexpected token <"?

本文关键字:意外 lt SyntaxError 错误 为什么      更新时间:2023-09-26

这是我正在使用的代码:

<?php 
$pattern="('.jpg$)|('.png$)|('.jpeg$)|('.gif$) |('.Gif$)"; //valid image extensions 
$files = array(); 
$curimage=0; 
if($handle = opendir($"http://newsxpressmedia.com/files/radar-simulation-files")) { 
    while(false !== ($file = readdir($handle))){ 
        if(eregi($pattern, $file)){ //if this file is a valid image 
            //Output it as a JavaScript array element 
            $files[] = $file; 
            $curimage++; 
        } 
    } 
    closedir($handle); 
} 
?> 
<!DOCTYPE html>
<html>
   <head>
      <title>change picture</title>
      <link rel="stylesheet" type="text/css" href="/css/result-light.css">
  <style type='text/css'>
    #Timer_Countdown{
    background:black;
    color:yellow;
    font-weight:bold;
    text-align:center;
}
  </style>
      <script type = "text/javascript">
          function displayNextImage() {
              x = (x === images.length - 1) ? 0 : x + 1;
              document.getElementById("img").src = images[x];
          }
          function displayPreviousImage() {
              x = (x <= 0) ? images.length - 1 : x - 1;
              document.getElementById("img").src = images[x];
          }
var images = <?=json_encode($files)?>;
//var images = [];
var x = -1; 
var swap_hours = 0;
var swap_minutes = 0;
var swap_seconds = 5;
var down_counter_hours;
var down_counter_minutes;
var down_counter_seconds;
function initTimer() {
    down_counter_hours = swap_hours;
    down_counter_minutes = swap_minutes;
    down_counter_seconds = swap_seconds;
    counter = setInterval(switcher, 1000);
}
function restartCounter() {
        down_counter_hours = swap_hours;
        down_counter_minutes = swap_minutes;
        down_counter_seconds = swap_seconds;
}
function switcher() {
    down_counter_seconds--;
    if (down_counter_hours <= 0 && down_counter_minutes <= 0 && down_counter_seconds <= 0) {
        swapColor();
        restartCounter();
    }
    if (down_counter_seconds <= 0 && down_counter_minutes > 0) {
        down_counter_seconds = 60;
        down_counter_minutes--;
    }
    if (down_counter_minutes <= 0 && down_counter_hours > 0) {
        down_counter_minutes = 60;
        down_counter_hours--;
    }
    document.getElementById("Timer_Countdown").innerText =        down_counter_hours+":"+down_counter_minutes+":"+down_counter_seconds;
}
function swapColor() {
    displayNextImage();
}
      </script>
      <div id="div_hours" class="div_box"></div>
      <div id="div_minutes" class="div_box"></div>
      <div id="div_seconds" class="div_box"></div>
      <div id="div_switcher" class="div_box"></div>
   </head>
   <body onload = "initTimer()">
       <div id="Timer_Countdown">&nbsp;</div>
       <img id="img" src="http://newsxpressmedia.com/files/theme/radar000005.Gif">
       <button onclick="displayPreviousImage(); restartCounter()">Previous</button>
       <button onclick="displayNextImage(); restartCounter()">Next</button>
   </body>
</html>

错误出现在线路上:

var images = <?=json_encode($files)?>;

如果我把这行改成这行:

var images = [];

然后代码运行良好,但没有使用php文件变量。线路出现问题:var images = <?=json_encode($files)?>;

我试图将此行更改为:echo json_encode($images);var images = echo json_encode($files);,但出现了相同的错误。

我正在使用weebly来构建我的网站,我的网站服务器在ipage.com 上

如何修复错误?

看起来您没有启用短标签(并且使用的是5.4 php之前的版本),因此行<?=json_encode($files)?>不会被php解析,只是直接发送。var images=<。。。不是有效的javascript表达式。以下是关于php的short_open_tag参数的一些信息:http://php.net/manual/en/ini.core.php#ini.short-打开标签

一些更改。更改

$pattern ="('.jpg$)|('.png$)|('.jpeg$)|('.gif$) |('.Gif$)";

$pattern = '/'.(png|jpg|jpeg|gif)$/i';

eregi($pattern, $file)

preg_match($pattern, $file)

看看会发生什么。eregi被弃用为PHP 5.3.0。

您在PHP标记中使用'='。试试这个:

var images = <?json_encode($files)?>;