如何在php中使用javascript和处理文件
这是我代码的一部分,我需要从一个文件中读取,我需要对行进行一些操作(拆分字符串),然后在表中打印信息 所以我需要使用java脚本来处理字符串,但是我的代码因为脚本标记()而无法工作 那么,如何在php中使用javascript并同时处理文件呢如何在php中使用javascript和处理文件,javascript,php,file,Javascript,Php,File,这是我代码的一部分,我需要从一个文件中读取,我需要对行进行一些操作(拆分字符串),然后在表中打印信息 所以我需要使用java脚本来处理字符串,但是我的代码因为脚本标记()而无法工作 那么,如何在php中使用javascript并同时处理文件呢 <?php $myfile = fopen("scores.txt", "r"); echo "<script>"; echo "<table border=1 ><tr><th>
<?php
$myfile = fopen("scores.txt", "r");
echo "<script>";
echo "<table border=1 ><tr><th> Home Team </th><th> Home Team Score </th> <th> Away Team Score </th> <th> Away Team </th></tr>";
while(!feof($myfile)) {
$str = fgets($myfile);
echo " var temp = $str.split(':');";
echo "<tr> <td> temp[0] </td> <td> temp[2] </td><td> temp[3] </td> <td> temp[1]</td></tr>";
}
echo "</table></script>";
fclose($myfile);
?>
您正在混合使用php和javascript。那不行。
但是这里不需要Javascript。
如果文件操作正确,则应该可以:
<?php
$myfile = fopen("scores.txt", "r");
echo "<table border=1 ><tr><th> Home Team </th><th> Home Team Score </th> <th> Away Team Score </th> <th> Away Team </th></tr>";
while(!feof($myfile)) {
$str = fgets($myfile);
$temp = explode(':', $str);
echo "<tr><td>". $temp[0] ."</td><td>". $temp[2] ."</td><td>". temp[3] ."</td><td>". temp[1] ."</td></tr>";
}
echo "</table>";
fclose($myfile);
?>
您不能在
标记中使用
这应该在php中完成,而不是在js中不管怎么说,你把它搞混了。啊哈,这就是问题所在?那你能告诉我怎么处理吗?你在做JS模板吗?或者,使用script标记背后的原因是什么?您正在混合和加工两种不同的编程语言$temp=explode(“:”,$str)
,然后使用$temp[0]
等等。请删除$tmp前面的var
,不要紧:)编辑:在那里留下了“var”,现在不见了@是的,我注意到了。。。