脚本适用于 php 7 但不适用于 php 5
script works in php 7 but not php 5
此代码适用于 php 7
(localhost) 但不适用于 php 5
(isp)。它将带分隔符的文本文件转换为 json
文件以用于 dataTables
.
来源:
6590|07/19/2003|RCC Sat. Open|C11|Nikolayev, Igor (FM)|2402|Gonzalez, Jose|2131|1-0|
想要的结果:
{"data":[
{"game":"6624","Date":"11/01/2003","Event":"RCC Sat. Open","ECO":"C65","White":"Liman, Christ","WhiteElo":"1729","Black":"Nikolayev, Igor (FM)","BlackElo":"2408","Result":"0-1"},
....
]}
怎么会?
<?php
$text = file('games.txt'); $count = count($text); arsort($text);
$X = 0;
$fp = fopen("games.json", "w");
fwrite ($fp, "{"); fwrite ($fp, '"data":[');
foreach($text as $line) {$token = explode("|", $line);
$game = $token[0]; $date = $token[1]; $event = $token[2]; $eco = $token[3]; $white = $token[4];
$white_rating = $token[5]; $black = $token[6]; $black_rating = $token[7]; $result = $token[8];
fwrite ($fp, "\n");
fwrite ($fp,'{');
fwrite ($fp, '"game":"'.$game.'",');
fwrite ($fp, '"Date":"'.$date.'",');
fwrite ($fp, '"Event":"'.$event.'",');
fwrite ($fp, '"ECO":"'.$eco.'",');
fwrite ($fp, '"White":"'.$white.'",');
fwrite ($fp, '"WhiteElo":"'.$white_rating.'",');
fwrite ($fp, '"Black":"'.$black.'",');
fwrite ($fp, '"BlackElo":"'.$black_rating.'",');
fwrite ($fp, '"Result":"'.$result.'"');
$X++;
fwrite ($fp, '}');
if ( $X <= $count-1 ) {fwrite ($fp, ',');}
}
fwrite ($fp, "]");
fwrite ($fp, "}");
fclose($fp);
header('Location:pgn_assistant.php');
?>
使用 PHP 文档,您可以使用 json_encode
这是将数组或任何实现 \JsonSerializable
的对象编码为 json 字符串的原生方式。
file_put_contents('myfile.json', json_encode($data));
此代码适用于 php 7
(localhost) 但不适用于 php 5
(isp)。它将带分隔符的文本文件转换为 json
文件以用于 dataTables
.
来源:
6590|07/19/2003|RCC Sat. Open|C11|Nikolayev, Igor (FM)|2402|Gonzalez, Jose|2131|1-0|
想要的结果:
{"data":[
{"game":"6624","Date":"11/01/2003","Event":"RCC Sat. Open","ECO":"C65","White":"Liman, Christ","WhiteElo":"1729","Black":"Nikolayev, Igor (FM)","BlackElo":"2408","Result":"0-1"},
....
]}
怎么会?
<?php
$text = file('games.txt'); $count = count($text); arsort($text);
$X = 0;
$fp = fopen("games.json", "w");
fwrite ($fp, "{"); fwrite ($fp, '"data":[');
foreach($text as $line) {$token = explode("|", $line);
$game = $token[0]; $date = $token[1]; $event = $token[2]; $eco = $token[3]; $white = $token[4];
$white_rating = $token[5]; $black = $token[6]; $black_rating = $token[7]; $result = $token[8];
fwrite ($fp, "\n");
fwrite ($fp,'{');
fwrite ($fp, '"game":"'.$game.'",');
fwrite ($fp, '"Date":"'.$date.'",');
fwrite ($fp, '"Event":"'.$event.'",');
fwrite ($fp, '"ECO":"'.$eco.'",');
fwrite ($fp, '"White":"'.$white.'",');
fwrite ($fp, '"WhiteElo":"'.$white_rating.'",');
fwrite ($fp, '"Black":"'.$black.'",');
fwrite ($fp, '"BlackElo":"'.$black_rating.'",');
fwrite ($fp, '"Result":"'.$result.'"');
$X++;
fwrite ($fp, '}');
if ( $X <= $count-1 ) {fwrite ($fp, ',');}
}
fwrite ($fp, "]");
fwrite ($fp, "}");
fclose($fp);
header('Location:pgn_assistant.php');
?>
使用 PHP 文档,您可以使用 json_encode
这是将数组或任何实现 \JsonSerializable
的对象编码为 json 字符串的原生方式。
file_put_contents('myfile.json', json_encode($data));