使用 PHP 通过 JSON 字符串逐行读取
Reading Line by Line Using PHP Via JSON String
我正在尝试创建一个 PHP 函数,它允许我使用 JSON 字符串从文本文件中读取选定的行。 JSON 字符串由两个用逗号分隔的数字组成。第一个数字是我要查找的行,第二个是向下读取的行数。
例如,我将这句话放在一个单独的文本文件中:
A diving spiral attack in which Pikachu head butts downward while
being surrounded by electricity.
(如果您想知道,这是我即将推出的粉丝网站的一部分,仍在建设中)。
句子从第6行开始,用完2行。所以在我的 JSON 文件中,我会写“6,2”。 explode() 命令是 运行 使用逗号作为分隔符,允许我将这些数字存储到两个单独的变量中,$start 和 $length。
$result = explode(",", $sample); // In JSON string, $sample is $i["desc"]
$start = $result[0];
$length = $result[1];
有了现在的变量,接下来的行将被调用以执行实际的逐行提取。
$file = new SplFileObject("pikachu/pikachu.txt");
for ($i = $start; $i <= ($start+$length); $i++) {
$file->seek($i-1);
echo "$file";
}
我使用直接调用而不是通过 JSON 解析对我的脚本进行了测试。那奏效了。但是当我尝试使用 JSON 字符串时,我遇到了运行时错误和致命错误。
如何修复脚本,以便我可以使用 JSON 字符串作为标记,从外部文本中寻找要打印的行,同时避免运行时错误和致命错误?
(编辑:这个问题对我来说太难解决了,所以我决定暂时放弃逐行搜索功能。我会在以后重新讨论这个问题。我会留下代码以下用于存档目的。)
character.php
<?php
$name = $_GET['name'];
// Step 1: Get the name from the previous page and store it.
$file = file_get_contents("characters/$name/$name.json");
// Step 2: Use the name to find a JSON file for the character (We'll do the moves list later).
$json = json_decode($file, true);
// Step 3: Decode the JSON file
$character = $json["character"][0]["name"];
$group = $json["character"][0]["group"];
$series = $json["character"][0]["series"];
$story = $json["character"][0]["story"];
$snapback = $json["character"][0]["snapback"];
$interrupt = $json["character"][0]["interrupt"];
$hiddenpow = $json["character"][0]["hiddenpow"];
$disrupt = $json["character"][0]["disrupt"];
$assist1 = $json["character"][0]["assist1"];
$assist2 = $json["character"][0]["assist2"];
$assist3 = $json["character"][0]["assist3"];
$counter = $json["character"][0]["counter"];
$bros = $json["character"][0]["bros"];
$party = $json["character"][0]["party"];
$ground = $json["character"][0]["ground"];
$jump = $json["character"][0]["jump"];
$superjump = $json["character"][0]["superjump"];
$launcher = $json["character"][0]["launcher"];
$altlaunch = $json["character"][0]["altlaunch"];
$acfinisher = $json["character"][0]["acfinisher"];
// Step 4: Retrieve variables and store them for use
echo "<HTML>\n";
echo "<HEAD>\n";
echo "<TITLE>Marvel & Capcom vs. Pokemon: $character</TITLE>\n\n";
echo "<LINK rel=\"stylesheet\" type=\"text/css\" href=\"http://fonts.googleapis.com/css?family=Roboto\">\n";
echo "<LINK rel=\"stylesheet\" type=\"text/css\" href=\"http://fonts.googleapis.com/css?family=Open+Sans\">\n";
echo "<LINK rel=\"stylesheet\" type=\"text/css\" href=\"media/style.css\">\n";
echo "<SCRIPT type=\"text/javascript\" src=\"media/tabs.js\"></SCRIPT>\n";
echo "<SCRIPT type=\"text/javascript\">\n";
echo "function toggle_visibility(id) {\n";
echo " var movename = id.nextSibling.nextSibling;\n";
echo " if(movename.style.display == \"block\")\n";
echo " movename.style.display = \"none\";\n";
echo " else\n";
echo " movename.style.display = \"block\";\n";
echo "}\n";
echo "</SCRIPT>";
echo "</HEAD>\n\n";
echo "<BODY onload=\"init()\">\n\n";
// Step 5: The HTML stuff begins
echo "<TABLE id=\"Character\">\n";
echo " <TR>\n";
echo " <TD class=\"Name\">" . strtoupper($character) . "</TD>\n";
echo " <TD class=\"Image\" rowspan=\"4\"><IMG src=\"characters/$name/$name.jpg\"></TD></TR>\n";
echo " <TR>\n";
echo " <TD class=\"Series\">Series: $series</TD></TR>\n";
echo " <TR>\n";
echo " <TD class=\"Story\">$story</TD></TR>\n";
// Step 6: Write the variables to the file
echo " <TR>\n";
echo " <TD style=\"vertical-align: top\">\n";
echo " <TABLE id=\"$group\">\n";
function moverw ($move) {
$subfollow = "FOLLOW>>";
$follow = "FOLLOW>";
if (strpos($move,$subfollow) !== false)
$move .= " (followup)";
else if (strpos($move,$follow) !== false)
$move .= " (followup)";
$move = str_replace("FOLLOW>>", "<IMG class=\"Followup\" src=\"images/follow.png\" style=\"text-indent: 5px\"> ", $move);
$move = str_replace("FOLLOW>", "<IMG class=\"Followup\" src=\"images/follow.png\"> ", $move);
return ($move);
}
function descrw ($desc,$name) {
$result = explode(",", $desc);
$start = $result[0];
$length = $result[1];
$file = new SplFileObject("characters/$name/$name.txt");
$result = "";
for ($i = $start; $i <= ($start+$length); $i++) {
$file->seek($i-1);
$result .= $file;
}
return($result);
}
function commrw ($command) {
$command = str_replace("AM,", "<IMG title=\"Assist Mode\" src=\"images/assist.gif\"> ", $command);
$command = str_replace("SM,", "<IMG title=\"Single Mode\" src=\"images/single.gif\"> ", $command);
$command = str_replace("BRAWL,", "<IMG title=\"Super Smash Brawl\" src=\"images/brawl.png\"> ", $command);
$command = str_replace("uf,", "<IMG class=\"Arrow\" src=\"images/uf.png\">", $command);
$command = str_replace("df,", "<IMG class=\"Arrow\" src=\"images/df.png\">", $command);
$command = str_replace("ub,", "<IMG class=\"Arrow\" src=\"images/ub.png\">", $command);
$command = str_replace("db,", "<IMG class=\"Arrow\" src=\"images/db.png\">", $command);
$command = str_replace("u,", "<IMG class=\"Arrow\" src=\"images/u.png\">", $command);
$command = str_replace("f,", "<IMG class=\"Arrow\" src=\"images/f.png\">", $command);
$command = str_replace("d,", "<IMG class=\"Arrow\" src=\"images/d.png\">", $command);
$command = str_replace("b,", "<IMG class=\"Arrow\" src=\"images/b.png\">", $command);
$command = str_replace("+", "<IMG class=\"Plus\" src=\"images/plus.png\">", $command);
$command = str_replace("AP", "<IMG class=\"Button\" src=\"images/punch.png\">", $command);
$command = str_replace("AK", "<IMG class=\"Button\" src=\"images/kick.png\">", $command);
$command = str_replace("A1", "<IMG class=\"Button\" title=\"Assist 1\" src=\"images/assist1.png\">", $command);
$command = str_replace("A2", "<IMG class=\"Button\" title=\"Assist 2\" src=\"images/assist2.png\">", $command);
$command = str_replace("AB", "<IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\">", $command);
$command = str_replace("LP", "<IMG class=\"Button\" title=\"Jab\" src=\"images/jab.png\">", $command);
$command = str_replace("LK", "<IMG class=\"Button\" title=\"Short\" src=\"images/short.png\">", $command);
$command = str_replace("MP", "<IMG class=\"Button\" title=\"Strong\" src=\"images/strong.png\">", $command);
$command = str_replace("MK", "<IMG class=\"Button\" title=\"Forward\" src=\"images/forward.png\">", $command);
$command = str_replace("HP", "<IMG class=\"Button\" title=\"Fierce\" src=\"images/fierce.png\">", $command);
$command = str_replace("HK", "<IMG class=\"Button\" title=\"Roundhouse\" src=\"images/roundhouse.png\">", $command);
$command = str_replace("FOLLOW>>", "<IMG class=\"Followup\" src=\"images/follow.png\" style=\"text-indent: 5px\"> ", $command);
$command = str_replace("FOLLOW>", "<IMG class=\"Followup\" src=\"images/follow.png\"> ", $command);
$command = str_replace("degrees", "°", $command);
return ($command);
}
function typerw ($type) {
$type = str_replace("BUG.", "<IMG src=\"images/bug.png\">", $type);
$type = str_replace("CYBER.", "<IMG src=\"images/cyber.png\">", $type);
$type = str_replace("DARK.", "<IMG src=\"images/dark.png\">", $type);
$type = str_replace("DRAGON.", "<IMG src=\"images/dragon.png\">", $type);
$type = str_replace("ELECTRIC.", "<IMG src=\"images/electric.png\">", $type);
$type = str_replace("FAIRY.", "<IMG src=\"images/fairy.png\">", $type);
$type = str_replace("FIGHTING.", "<IMG src=\"images/fighting.png\">", $type);
$type = str_replace("FIRE.", "<IMG src=\"images/fire.png\">", $type);
$type = str_replace("FLYING.", "<IMG src=\"images/flying.png\">", $type);
$type = str_replace("GHOST.", "<IMG src=\"images/ghost.png\">", $type);
$type = str_replace("GRASS.", "<IMG src=\"images/grass.png\">", $type);
$type = str_replace("GROUND.", "<IMG src=\"images/ground.png\">", $type);
$type = str_replace("ICE.", "<IMG src=\"images/ice.png\">", $type);
$type = str_replace("LIGHT.", "<IMG src=\"images/light.png\">", $type);
$type = str_replace("NORMAL.", "<IMG src=\"images/normal.png\">", $type);
$type = str_replace("PSYCHIC.", "<IMG src=\"images/psychic.png\">", $type);
$type = str_replace("STEEL.", "<IMG src=\"images/steel.png\">", $type);
$type = str_replace("WATER.", "<IMG src=\"images/water.png\">", $type);
return ($type);
}
function noterw ($note) {
$note = str_replace(".AM.", "<IMG title=\"Assist Mode\" src=\"images/assist.gif\"> ", $note);
$note = str_replace(".SM.", "<IMG title=\"Single Mode\" src=\"images/single.gif\"> ", $note);
$note = str_replace(".HP.", "<IMG class=\"HiddenPower\" title=\"Hidden Power\"src=\"images/hiddenpower.png\">", $note);
return ($note);
}
foreach ($json["character"][0]["moves"] as $i) {
if ($i["flag"] == "normal") {
$move = strtoupper($i["move"]);
$desc = $i["desc"];
$command = $i["comm"];
$type = $i["type"];
$note = $i["note"];
echo " <TR>\n";
echo " <TD class=\"MoveName Normal\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
echo " <DIV class=\"Details\">" . descrw($desc,$name) . "</DIV></TD>\n";
echo " <TD class=\"Command\">" . commrw($command) . "</TD>\n";
echo " <TD class=\"Type\">" . typerw($type) . "</TD>\n";
echo " <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
}
if ($i["flag"] == "special") {
$move = strtoupper($i["move"]);
$command = $i["comm"];
$type = $i["type"];
$note = $i["note"];
echo " <TR>\n";
echo " <TD class=\"MoveName Special\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
echo " <DIV class=\"Details\">" . $i["desc"] . "</DIV></TD>\n";
echo " <TD class=\"Command\">" . commrw($command) . "</TD>\n";
echo " <TD class=\"Type\">" . typerw($type) . "</TD>\n";
echo " <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
}
if ($i["flag"] == "hyper") {
$move = strtoupper($i["move"]);
$command = $i["comm"];
$type = $i["type"];
$note = $i["note"];
echo " <TR>\n";
echo " <TD class=\"MoveName Hyper\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
echo " <DIV class=\"Details\">" . $i["desc"] . "</DIV></TD>\n";
echo " <TD class=\"Command\">" . commrw($command) . "</TD>\n";
echo " <TD class=\"Type\">" . typerw($type) . "</TD>\n";
echo " <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
}
if ($i["flag"] == "final") {
$move = strtoupper($i["move"]);
$command = $i["comm"];
$type = $i["type"];
$note = $i["note"];
echo " <TR>\n";
echo " <TD class=\"MoveName Final\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
echo " <DIV id=\"Final\" class=\"Details\">" . $i["desc"] . "</DIV></TD>\n";
echo " <TD class=\"Command\">" . commrw($command) . "</TD>\n";
echo " <TD class=\"Type\">" . typerw($type) . "</TD>\n";
echo " <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
}
}
// Step 7: Write the moves. When Command and Type are parsed, execute respective rewriters. Saves me from typing lots, hun ;)
echo " </TABLE></TD></TR>\n";
echo " <TR>\n";
echo " <TD colspan=\"2\">\n";
echo " <UL id=\"tabs\">\n";
echo " <LI><A href=\"#Single\" onclick=\"showTab()\">SINGLE MODE</A></LI>\n";
echo " <LI><A href=\"#Assist\" onclick=\"showTab()\">ASSIST MODE</A></LI>\n";
echo " <LI><A href=\"#Combo\" onclick=\"showTab()\">COMBO PROFILE</A></LI>\n";
echo " <LI><A href=\"#Attack\" onclick=\"showTab()\">ATTACK PROFILE</A></LI></UL>\n";
echo " <DIV class=\"tabContent\" id=\"Single\">\n";
echo " <TABLE id=\"Single\" border=1>\n";
echo " <TR>\n";
echo " <TH>Snapback:</TH>\n";
echo " <TD>$snapback</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Interrupt:</TH>\n";
echo " <TD>$interrupt</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Hidden Power:</TH>\n";
echo " <TD>$hiddenpow</TD></TR>\n";
echo " <TR>\n";
echo " <TH class=\"Header Rumble\"colspan=\"2\">RUMBLE CHART</TH></TR>\n";
echo " <TR>\n";
echo " <TD colspan=\"2\"></TH></TR></TABLE></DIV>\n";
echo " <DIV class=\"tabContent\" id=\"Assist\">\n";
echo " <TABLE id=\"Assist\" border=1>\n";
echo " <TR>\n";
echo " <TH>Disrupt:</TH>\n";
echo " <TD>$disrupt</TD></TR>\n";
echo " <TR>\n";
echo " <TH colspan=2>Assist Attacks</TH></TR>\n";
echo " <TR>\n";
echo " <TH><IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\"></TH>\n";
echo " <TD>$assist1</TD></TR>\n";
echo " <TR>\n";
echo " <TH><IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\"><IMG class=\"Plus\" src=\"images/plus.png\"><IMG class=\"Button\" title=\"Fierce\" src=\"images/fierce.png\"></TH>\n";
echo " <TD>$assist2</TD></TR>\n";
echo " <TR>\n";
echo " <TH><IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\"><IMG class=\"Plus\" src=\"images/plus.png\"><IMG class=\"Button\" title=\"Roundhouse\" src=\"images/roundhouse.png\"></TH>\n";
echo " <TD>$assist3</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Cross-Over Counter:</TH>\n";
echo " <TD>$counter</TD></TR>\n";
echo " <TR>\n";
echo " <TH colspan=2>Super Smash Melee</TH></TR>\n";
echo " <TR>\n";
echo " <TH>Bros. Attack:</TH>\n";
echo " <TD>$bros</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Party Attack:</TH>\n";
echo " <TD>$party</TD></TR></TABLE></DIV>\n";
echo " <DIV class=\"tabContent\" id=\"Combo\">\n";
echo " <TABLE id=\"Combo\" border=1>\n";
echo " <TR>\n";
echo " <TH colspan=2>???</TH></TR>\n";
echo " <TR>\n";
echo " <TH>Ground:</TH>\n";
echo " <TD>$ground</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Jump:</TH>\n";
echo " <TD>$jump</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Super Jump:</TH>\n";
echo " <TD>$superjump</TD></TR>\n";
echo " <TR>\n";
echo " <TH colspan=2></TH></TR>\n";
echo " <TR>\n";
echo " <TH>Launcher:</TH>\n";
echo " <TD>$launcher</TD></TR>\n";
if ($altlaunch != null) {
echo " <TR>\n";
echo " <TH>Alternate Launch:</TH>\n";
echo " <TD>$altlaunch</TD></TR>\n";
}
echo " <TR>\n";
echo " <TH>AC Finishers:</TH>\n";
echo " <TD>$acfinisher</TD></TR></TABLE></DIV>\n";
echo " <DIV class=\"tabContent\" id=\"Attack\">\n";
echo " $test 4\n";
echo " </DIV>\n";
echo " </TD></TR></TABLE></TD></TR></TABLE>\n\n";
echo "</BODY></HTML>";
?>
$name.json,其中$name为角色名称(以皮卡丘为例):
{"character":[
{
"name":"Pikachu",
"group":"Pokemon",
"series":"Pokemon",
"story":"To-do...",
"moves":[
{
"flag":"normal",
"move":"Discharge",
"desc":"3,2",
"comm":"Press HP (crouch) (air)",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"normal",
"move":"Electric Screw",
"desc":"6,2",
"comm":"Tilt d,+HP [air]",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"normal",
"move":"Tackle",
"desc":"9,1",
"comm":"Tap f, , f,",
"type":"NORMAL.",
"note":"Notes"
},
{
"flag":"special",
"move":"Quick Attack",
"desc":"11,2",
"comm":"Tap any direction and Press AP (air)",
"type":"ELECTRIC.FIGHTING.",
"note":"Notes"
},
{
"flag":"special",
"move":"Thunder Jolt",
"desc":"14",
"comm":"Motion d,df,f,+AP",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"special",
"move":"Electro Ball",
"desc":"16,2",
"comm":"Motion d,df,f,+AP [air]",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"special",
"move":"Thunder",
"desc":"19,2",
"comm":"Motion f,d,df,+AP (air)",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"special",
"move":"Electric Chair",
"desc":"22,2",
"comm":"Motion f,df,d,db,b,+AP",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"hyper",
"move":"Thunder Shock",
"desc":"25,2",
"comm":"Motion d,df,f,+APAP (air)",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"hyper",
"move":"Thunder Bolt",
"desc":"28,1",
"comm":"Motion d,df,f,+AKAK (air)",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"hyper",
"move":"Skull Bash",
"desc":"30,2",
"comm":"Motion d,db,b,+APAP (air)",
"type":"ELECTRIC.FIGHTING.",
"note":"Notes"
},
{
"flag":"final",
"move":"Volt Tackle",
"desc":"33,2",
"comm":"Motion d,df,f,d,df,f,+APAP (air)",
"type":"ELECTRIC.",
"note":"Notes"
}],
"snapback":"Crouching Forward",
"interrupt":"Thunder Jolt, Thunder",
"hiddenpow":"Hidden Power Enhancements (Line per line lookup)",
"disrupt":"Thunder",
"assist1":"Thunder Jolt",
"assist2":"Thunder",
"assist3":"Quick Attack",
"counter":"Thunder",
"bros":"Skull Bash",
"party":"Thunder Bolt",
"ground":"Punch to Kick",
"jump":"Punch to Kick",
"superjump":"Zig Zag",
"launcher":"Standing Roundhouse",
"altlaunch":"Crouching Strong",
"acfinisher":"Discharge, Electric Screw, Quick Attack, Electro Ball, Thunder, Thunder Shock, Thunder Bolt, Skull Bash"
}
]}
问题出在这个函数上,与你使用JSON无关。这只是一个简单的变量范围问题。
function descrw ($desc) {
$result = explode(",", $desc);
$start = $result[0];
$length = $result[1];
$file = new SplFileObject("$name/$name.txt");
for ($i = $start; $i <= ($start+$length); $i++) {
$file->seek($i-1);
echo "$file";
}
return(0);
}
变量 $name
未定义,因此它正在尝试打开文件 /.txt
。您需要更改函数,使其以 $name
作为参数。并且由于 descrw
的调用者希望它成为 return 一个字符串以便可以将其连接起来,因此 descrw
不应该自己做 echo
.
function descrw ($desc, $name) {
$result = explode(",", $desc);
$start = $result[0];
$length = $result[1];
$file = new SplFileObject("$name/$name.txt");
$result = '';
for ($i = $start; $i <= ($start+$length); $i++) {
$file->seek($i-1);
$result .= $file;
}
return($result);
}
然后改变调用传参的地方:
echo " <DIV class=\"Details\">" . descrw($desc, $name) . "</DIV></TD>\n";
我正在尝试创建一个 PHP 函数,它允许我使用 JSON 字符串从文本文件中读取选定的行。 JSON 字符串由两个用逗号分隔的数字组成。第一个数字是我要查找的行,第二个是向下读取的行数。
例如,我将这句话放在一个单独的文本文件中:
A diving spiral attack in which Pikachu head butts downward while
being surrounded by electricity.
(如果您想知道,这是我即将推出的粉丝网站的一部分,仍在建设中)。
句子从第6行开始,用完2行。所以在我的 JSON 文件中,我会写“6,2”。 explode() 命令是 运行 使用逗号作为分隔符,允许我将这些数字存储到两个单独的变量中,$start 和 $length。
$result = explode(",", $sample); // In JSON string, $sample is $i["desc"]
$start = $result[0];
$length = $result[1];
有了现在的变量,接下来的行将被调用以执行实际的逐行提取。
$file = new SplFileObject("pikachu/pikachu.txt");
for ($i = $start; $i <= ($start+$length); $i++) {
$file->seek($i-1);
echo "$file";
}
我使用直接调用而不是通过 JSON 解析对我的脚本进行了测试。那奏效了。但是当我尝试使用 JSON 字符串时,我遇到了运行时错误和致命错误。
如何修复脚本,以便我可以使用 JSON 字符串作为标记,从外部文本中寻找要打印的行,同时避免运行时错误和致命错误?
(编辑:这个问题对我来说太难解决了,所以我决定暂时放弃逐行搜索功能。我会在以后重新讨论这个问题。我会留下代码以下用于存档目的。)
character.php
<?php
$name = $_GET['name'];
// Step 1: Get the name from the previous page and store it.
$file = file_get_contents("characters/$name/$name.json");
// Step 2: Use the name to find a JSON file for the character (We'll do the moves list later).
$json = json_decode($file, true);
// Step 3: Decode the JSON file
$character = $json["character"][0]["name"];
$group = $json["character"][0]["group"];
$series = $json["character"][0]["series"];
$story = $json["character"][0]["story"];
$snapback = $json["character"][0]["snapback"];
$interrupt = $json["character"][0]["interrupt"];
$hiddenpow = $json["character"][0]["hiddenpow"];
$disrupt = $json["character"][0]["disrupt"];
$assist1 = $json["character"][0]["assist1"];
$assist2 = $json["character"][0]["assist2"];
$assist3 = $json["character"][0]["assist3"];
$counter = $json["character"][0]["counter"];
$bros = $json["character"][0]["bros"];
$party = $json["character"][0]["party"];
$ground = $json["character"][0]["ground"];
$jump = $json["character"][0]["jump"];
$superjump = $json["character"][0]["superjump"];
$launcher = $json["character"][0]["launcher"];
$altlaunch = $json["character"][0]["altlaunch"];
$acfinisher = $json["character"][0]["acfinisher"];
// Step 4: Retrieve variables and store them for use
echo "<HTML>\n";
echo "<HEAD>\n";
echo "<TITLE>Marvel & Capcom vs. Pokemon: $character</TITLE>\n\n";
echo "<LINK rel=\"stylesheet\" type=\"text/css\" href=\"http://fonts.googleapis.com/css?family=Roboto\">\n";
echo "<LINK rel=\"stylesheet\" type=\"text/css\" href=\"http://fonts.googleapis.com/css?family=Open+Sans\">\n";
echo "<LINK rel=\"stylesheet\" type=\"text/css\" href=\"media/style.css\">\n";
echo "<SCRIPT type=\"text/javascript\" src=\"media/tabs.js\"></SCRIPT>\n";
echo "<SCRIPT type=\"text/javascript\">\n";
echo "function toggle_visibility(id) {\n";
echo " var movename = id.nextSibling.nextSibling;\n";
echo " if(movename.style.display == \"block\")\n";
echo " movename.style.display = \"none\";\n";
echo " else\n";
echo " movename.style.display = \"block\";\n";
echo "}\n";
echo "</SCRIPT>";
echo "</HEAD>\n\n";
echo "<BODY onload=\"init()\">\n\n";
// Step 5: The HTML stuff begins
echo "<TABLE id=\"Character\">\n";
echo " <TR>\n";
echo " <TD class=\"Name\">" . strtoupper($character) . "</TD>\n";
echo " <TD class=\"Image\" rowspan=\"4\"><IMG src=\"characters/$name/$name.jpg\"></TD></TR>\n";
echo " <TR>\n";
echo " <TD class=\"Series\">Series: $series</TD></TR>\n";
echo " <TR>\n";
echo " <TD class=\"Story\">$story</TD></TR>\n";
// Step 6: Write the variables to the file
echo " <TR>\n";
echo " <TD style=\"vertical-align: top\">\n";
echo " <TABLE id=\"$group\">\n";
function moverw ($move) {
$subfollow = "FOLLOW>>";
$follow = "FOLLOW>";
if (strpos($move,$subfollow) !== false)
$move .= " (followup)";
else if (strpos($move,$follow) !== false)
$move .= " (followup)";
$move = str_replace("FOLLOW>>", "<IMG class=\"Followup\" src=\"images/follow.png\" style=\"text-indent: 5px\"> ", $move);
$move = str_replace("FOLLOW>", "<IMG class=\"Followup\" src=\"images/follow.png\"> ", $move);
return ($move);
}
function descrw ($desc,$name) {
$result = explode(",", $desc);
$start = $result[0];
$length = $result[1];
$file = new SplFileObject("characters/$name/$name.txt");
$result = "";
for ($i = $start; $i <= ($start+$length); $i++) {
$file->seek($i-1);
$result .= $file;
}
return($result);
}
function commrw ($command) {
$command = str_replace("AM,", "<IMG title=\"Assist Mode\" src=\"images/assist.gif\"> ", $command);
$command = str_replace("SM,", "<IMG title=\"Single Mode\" src=\"images/single.gif\"> ", $command);
$command = str_replace("BRAWL,", "<IMG title=\"Super Smash Brawl\" src=\"images/brawl.png\"> ", $command);
$command = str_replace("uf,", "<IMG class=\"Arrow\" src=\"images/uf.png\">", $command);
$command = str_replace("df,", "<IMG class=\"Arrow\" src=\"images/df.png\">", $command);
$command = str_replace("ub,", "<IMG class=\"Arrow\" src=\"images/ub.png\">", $command);
$command = str_replace("db,", "<IMG class=\"Arrow\" src=\"images/db.png\">", $command);
$command = str_replace("u,", "<IMG class=\"Arrow\" src=\"images/u.png\">", $command);
$command = str_replace("f,", "<IMG class=\"Arrow\" src=\"images/f.png\">", $command);
$command = str_replace("d,", "<IMG class=\"Arrow\" src=\"images/d.png\">", $command);
$command = str_replace("b,", "<IMG class=\"Arrow\" src=\"images/b.png\">", $command);
$command = str_replace("+", "<IMG class=\"Plus\" src=\"images/plus.png\">", $command);
$command = str_replace("AP", "<IMG class=\"Button\" src=\"images/punch.png\">", $command);
$command = str_replace("AK", "<IMG class=\"Button\" src=\"images/kick.png\">", $command);
$command = str_replace("A1", "<IMG class=\"Button\" title=\"Assist 1\" src=\"images/assist1.png\">", $command);
$command = str_replace("A2", "<IMG class=\"Button\" title=\"Assist 2\" src=\"images/assist2.png\">", $command);
$command = str_replace("AB", "<IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\">", $command);
$command = str_replace("LP", "<IMG class=\"Button\" title=\"Jab\" src=\"images/jab.png\">", $command);
$command = str_replace("LK", "<IMG class=\"Button\" title=\"Short\" src=\"images/short.png\">", $command);
$command = str_replace("MP", "<IMG class=\"Button\" title=\"Strong\" src=\"images/strong.png\">", $command);
$command = str_replace("MK", "<IMG class=\"Button\" title=\"Forward\" src=\"images/forward.png\">", $command);
$command = str_replace("HP", "<IMG class=\"Button\" title=\"Fierce\" src=\"images/fierce.png\">", $command);
$command = str_replace("HK", "<IMG class=\"Button\" title=\"Roundhouse\" src=\"images/roundhouse.png\">", $command);
$command = str_replace("FOLLOW>>", "<IMG class=\"Followup\" src=\"images/follow.png\" style=\"text-indent: 5px\"> ", $command);
$command = str_replace("FOLLOW>", "<IMG class=\"Followup\" src=\"images/follow.png\"> ", $command);
$command = str_replace("degrees", "°", $command);
return ($command);
}
function typerw ($type) {
$type = str_replace("BUG.", "<IMG src=\"images/bug.png\">", $type);
$type = str_replace("CYBER.", "<IMG src=\"images/cyber.png\">", $type);
$type = str_replace("DARK.", "<IMG src=\"images/dark.png\">", $type);
$type = str_replace("DRAGON.", "<IMG src=\"images/dragon.png\">", $type);
$type = str_replace("ELECTRIC.", "<IMG src=\"images/electric.png\">", $type);
$type = str_replace("FAIRY.", "<IMG src=\"images/fairy.png\">", $type);
$type = str_replace("FIGHTING.", "<IMG src=\"images/fighting.png\">", $type);
$type = str_replace("FIRE.", "<IMG src=\"images/fire.png\">", $type);
$type = str_replace("FLYING.", "<IMG src=\"images/flying.png\">", $type);
$type = str_replace("GHOST.", "<IMG src=\"images/ghost.png\">", $type);
$type = str_replace("GRASS.", "<IMG src=\"images/grass.png\">", $type);
$type = str_replace("GROUND.", "<IMG src=\"images/ground.png\">", $type);
$type = str_replace("ICE.", "<IMG src=\"images/ice.png\">", $type);
$type = str_replace("LIGHT.", "<IMG src=\"images/light.png\">", $type);
$type = str_replace("NORMAL.", "<IMG src=\"images/normal.png\">", $type);
$type = str_replace("PSYCHIC.", "<IMG src=\"images/psychic.png\">", $type);
$type = str_replace("STEEL.", "<IMG src=\"images/steel.png\">", $type);
$type = str_replace("WATER.", "<IMG src=\"images/water.png\">", $type);
return ($type);
}
function noterw ($note) {
$note = str_replace(".AM.", "<IMG title=\"Assist Mode\" src=\"images/assist.gif\"> ", $note);
$note = str_replace(".SM.", "<IMG title=\"Single Mode\" src=\"images/single.gif\"> ", $note);
$note = str_replace(".HP.", "<IMG class=\"HiddenPower\" title=\"Hidden Power\"src=\"images/hiddenpower.png\">", $note);
return ($note);
}
foreach ($json["character"][0]["moves"] as $i) {
if ($i["flag"] == "normal") {
$move = strtoupper($i["move"]);
$desc = $i["desc"];
$command = $i["comm"];
$type = $i["type"];
$note = $i["note"];
echo " <TR>\n";
echo " <TD class=\"MoveName Normal\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
echo " <DIV class=\"Details\">" . descrw($desc,$name) . "</DIV></TD>\n";
echo " <TD class=\"Command\">" . commrw($command) . "</TD>\n";
echo " <TD class=\"Type\">" . typerw($type) . "</TD>\n";
echo " <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
}
if ($i["flag"] == "special") {
$move = strtoupper($i["move"]);
$command = $i["comm"];
$type = $i["type"];
$note = $i["note"];
echo " <TR>\n";
echo " <TD class=\"MoveName Special\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
echo " <DIV class=\"Details\">" . $i["desc"] . "</DIV></TD>\n";
echo " <TD class=\"Command\">" . commrw($command) . "</TD>\n";
echo " <TD class=\"Type\">" . typerw($type) . "</TD>\n";
echo " <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
}
if ($i["flag"] == "hyper") {
$move = strtoupper($i["move"]);
$command = $i["comm"];
$type = $i["type"];
$note = $i["note"];
echo " <TR>\n";
echo " <TD class=\"MoveName Hyper\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
echo " <DIV class=\"Details\">" . $i["desc"] . "</DIV></TD>\n";
echo " <TD class=\"Command\">" . commrw($command) . "</TD>\n";
echo " <TD class=\"Type\">" . typerw($type) . "</TD>\n";
echo " <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
}
if ($i["flag"] == "final") {
$move = strtoupper($i["move"]);
$command = $i["comm"];
$type = $i["type"];
$note = $i["note"];
echo " <TR>\n";
echo " <TD class=\"MoveName Final\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
echo " <DIV id=\"Final\" class=\"Details\">" . $i["desc"] . "</DIV></TD>\n";
echo " <TD class=\"Command\">" . commrw($command) . "</TD>\n";
echo " <TD class=\"Type\">" . typerw($type) . "</TD>\n";
echo " <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
}
}
// Step 7: Write the moves. When Command and Type are parsed, execute respective rewriters. Saves me from typing lots, hun ;)
echo " </TABLE></TD></TR>\n";
echo " <TR>\n";
echo " <TD colspan=\"2\">\n";
echo " <UL id=\"tabs\">\n";
echo " <LI><A href=\"#Single\" onclick=\"showTab()\">SINGLE MODE</A></LI>\n";
echo " <LI><A href=\"#Assist\" onclick=\"showTab()\">ASSIST MODE</A></LI>\n";
echo " <LI><A href=\"#Combo\" onclick=\"showTab()\">COMBO PROFILE</A></LI>\n";
echo " <LI><A href=\"#Attack\" onclick=\"showTab()\">ATTACK PROFILE</A></LI></UL>\n";
echo " <DIV class=\"tabContent\" id=\"Single\">\n";
echo " <TABLE id=\"Single\" border=1>\n";
echo " <TR>\n";
echo " <TH>Snapback:</TH>\n";
echo " <TD>$snapback</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Interrupt:</TH>\n";
echo " <TD>$interrupt</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Hidden Power:</TH>\n";
echo " <TD>$hiddenpow</TD></TR>\n";
echo " <TR>\n";
echo " <TH class=\"Header Rumble\"colspan=\"2\">RUMBLE CHART</TH></TR>\n";
echo " <TR>\n";
echo " <TD colspan=\"2\"></TH></TR></TABLE></DIV>\n";
echo " <DIV class=\"tabContent\" id=\"Assist\">\n";
echo " <TABLE id=\"Assist\" border=1>\n";
echo " <TR>\n";
echo " <TH>Disrupt:</TH>\n";
echo " <TD>$disrupt</TD></TR>\n";
echo " <TR>\n";
echo " <TH colspan=2>Assist Attacks</TH></TR>\n";
echo " <TR>\n";
echo " <TH><IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\"></TH>\n";
echo " <TD>$assist1</TD></TR>\n";
echo " <TR>\n";
echo " <TH><IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\"><IMG class=\"Plus\" src=\"images/plus.png\"><IMG class=\"Button\" title=\"Fierce\" src=\"images/fierce.png\"></TH>\n";
echo " <TD>$assist2</TD></TR>\n";
echo " <TR>\n";
echo " <TH><IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\"><IMG class=\"Plus\" src=\"images/plus.png\"><IMG class=\"Button\" title=\"Roundhouse\" src=\"images/roundhouse.png\"></TH>\n";
echo " <TD>$assist3</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Cross-Over Counter:</TH>\n";
echo " <TD>$counter</TD></TR>\n";
echo " <TR>\n";
echo " <TH colspan=2>Super Smash Melee</TH></TR>\n";
echo " <TR>\n";
echo " <TH>Bros. Attack:</TH>\n";
echo " <TD>$bros</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Party Attack:</TH>\n";
echo " <TD>$party</TD></TR></TABLE></DIV>\n";
echo " <DIV class=\"tabContent\" id=\"Combo\">\n";
echo " <TABLE id=\"Combo\" border=1>\n";
echo " <TR>\n";
echo " <TH colspan=2>???</TH></TR>\n";
echo " <TR>\n";
echo " <TH>Ground:</TH>\n";
echo " <TD>$ground</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Jump:</TH>\n";
echo " <TD>$jump</TD></TR>\n";
echo " <TR>\n";
echo " <TH>Super Jump:</TH>\n";
echo " <TD>$superjump</TD></TR>\n";
echo " <TR>\n";
echo " <TH colspan=2></TH></TR>\n";
echo " <TR>\n";
echo " <TH>Launcher:</TH>\n";
echo " <TD>$launcher</TD></TR>\n";
if ($altlaunch != null) {
echo " <TR>\n";
echo " <TH>Alternate Launch:</TH>\n";
echo " <TD>$altlaunch</TD></TR>\n";
}
echo " <TR>\n";
echo " <TH>AC Finishers:</TH>\n";
echo " <TD>$acfinisher</TD></TR></TABLE></DIV>\n";
echo " <DIV class=\"tabContent\" id=\"Attack\">\n";
echo " $test 4\n";
echo " </DIV>\n";
echo " </TD></TR></TABLE></TD></TR></TABLE>\n\n";
echo "</BODY></HTML>";
?>
$name.json,其中$name为角色名称(以皮卡丘为例):
{"character":[
{
"name":"Pikachu",
"group":"Pokemon",
"series":"Pokemon",
"story":"To-do...",
"moves":[
{
"flag":"normal",
"move":"Discharge",
"desc":"3,2",
"comm":"Press HP (crouch) (air)",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"normal",
"move":"Electric Screw",
"desc":"6,2",
"comm":"Tilt d,+HP [air]",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"normal",
"move":"Tackle",
"desc":"9,1",
"comm":"Tap f, , f,",
"type":"NORMAL.",
"note":"Notes"
},
{
"flag":"special",
"move":"Quick Attack",
"desc":"11,2",
"comm":"Tap any direction and Press AP (air)",
"type":"ELECTRIC.FIGHTING.",
"note":"Notes"
},
{
"flag":"special",
"move":"Thunder Jolt",
"desc":"14",
"comm":"Motion d,df,f,+AP",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"special",
"move":"Electro Ball",
"desc":"16,2",
"comm":"Motion d,df,f,+AP [air]",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"special",
"move":"Thunder",
"desc":"19,2",
"comm":"Motion f,d,df,+AP (air)",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"special",
"move":"Electric Chair",
"desc":"22,2",
"comm":"Motion f,df,d,db,b,+AP",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"hyper",
"move":"Thunder Shock",
"desc":"25,2",
"comm":"Motion d,df,f,+APAP (air)",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"hyper",
"move":"Thunder Bolt",
"desc":"28,1",
"comm":"Motion d,df,f,+AKAK (air)",
"type":"ELECTRIC.",
"note":"Notes"
},
{
"flag":"hyper",
"move":"Skull Bash",
"desc":"30,2",
"comm":"Motion d,db,b,+APAP (air)",
"type":"ELECTRIC.FIGHTING.",
"note":"Notes"
},
{
"flag":"final",
"move":"Volt Tackle",
"desc":"33,2",
"comm":"Motion d,df,f,d,df,f,+APAP (air)",
"type":"ELECTRIC.",
"note":"Notes"
}],
"snapback":"Crouching Forward",
"interrupt":"Thunder Jolt, Thunder",
"hiddenpow":"Hidden Power Enhancements (Line per line lookup)",
"disrupt":"Thunder",
"assist1":"Thunder Jolt",
"assist2":"Thunder",
"assist3":"Quick Attack",
"counter":"Thunder",
"bros":"Skull Bash",
"party":"Thunder Bolt",
"ground":"Punch to Kick",
"jump":"Punch to Kick",
"superjump":"Zig Zag",
"launcher":"Standing Roundhouse",
"altlaunch":"Crouching Strong",
"acfinisher":"Discharge, Electric Screw, Quick Attack, Electro Ball, Thunder, Thunder Shock, Thunder Bolt, Skull Bash"
}
]}
问题出在这个函数上,与你使用JSON无关。这只是一个简单的变量范围问题。
function descrw ($desc) {
$result = explode(",", $desc);
$start = $result[0];
$length = $result[1];
$file = new SplFileObject("$name/$name.txt");
for ($i = $start; $i <= ($start+$length); $i++) {
$file->seek($i-1);
echo "$file";
}
return(0);
}
变量 $name
未定义,因此它正在尝试打开文件 /.txt
。您需要更改函数,使其以 $name
作为参数。并且由于 descrw
的调用者希望它成为 return 一个字符串以便可以将其连接起来,因此 descrw
不应该自己做 echo
.
function descrw ($desc, $name) {
$result = explode(",", $desc);
$start = $result[0];
$length = $result[1];
$file = new SplFileObject("$name/$name.txt");
$result = '';
for ($i = $start; $i <= ($start+$length); $i++) {
$file->seek($i-1);
$result .= $file;
}
return($result);
}
然后改变调用传参的地方:
echo " <DIV class=\"Details\">" . descrw($desc, $name) . "</DIV></TD>\n";