-3

我找到了剧本。 http://www.aota.net/forums/showthread.php?t=24155

我的 lang.php

<?php
$Lang = array(
'TEXT'      => "baer",
'GRET'      => "hallo",
'FACE'      => "face",
'HAPY'      => "happy",
'TOOL'      => "tool",
);

我的edit.php

<?
// edit the values of an array ($Lang["key"] = "value";) in the file below
$array_file = "lang.php";

// if POSTed, save file
if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
    unset($_POST['submit']);  // remove the submit button from the name/value pairs
    $new_array = "";          // initialize the new array string (file contents)
    $is_post = true;          // set flag for use below

    // add each form key/value pair to the file
    foreach (array_keys($_POST) as $key)
        { $new_array .= "\$Lang[\'$key\'] => \"".trim($_POST[$key])."\",\n"; }

    // write over the original file, and write a backup copy with the date/time
    write_file($array_file, $new_array);
    write_file($array_file . date("_Y-m-d_h-ia"), $new_array);
    }

// write a file
function write_file($filename, $contents)
    {
    if (! ($file = fopen($filename, 'w'))) { die("could not open $filename for writing"); }
    if (! (fwrite($file, $contents))) { die("could not write to $filename"); }
    fclose($file);
    }

// read the array into $Lang[]
$file_lines = file($array_file);
$Lang = array();
foreach ($file_lines as $line)
    {
    list($b4_key, $key, $b4_value, $value) = explode('"', $line);
    if (preg_match("/Lang/", $b4_key))
        { $Lang[$key] = $value; }
    }
?>    
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Language Editor</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1><?= $array_file ?></h1>
<? if (!isset($is_post)) { ?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
<? } ?>
<table>
<?
// loop through the $Lang array and display the Lang value (if POSTed) or a form element
foreach ($Lang as $key => $value)
    {
    echo "<tr><td>$key</td><td>";
    echo isset($is_post) ? "= \"$value\"": "<input type=\"text\" name=\"$key\" value=\"$value\">";
    echo "</td></tr>\n";
    }
    ?>
</table>
<? if (!isset($is_post)) { ?>
<p><input type="submit" name="submit" value="Save"></p>
</form>
<? } ?>
</body>
</html>

不幸的是,我无法从 lang.php 读取数组和键。

脚本编写者写道:array.txt 中确实设置了 $Bid [] 的所有行都将丢失。

但是我希望更改后的 lang.php 保存为 PHP 文件会去吗?

我想创建一个下拉列表并使用匹配的键加载数组,更改键文本并保存。

我提前感谢您的帮助!

4

1 回答 1

0

你不应该阅读 lang.php 文件。你应该包括它。

更好的方法:

require_once('lang.php'); // or require_once($array_file);

并删除这些行:

// read the array into $Lang[]
$file_lines = file($array_file);
$Lang = array();
foreach ($file_lines as $line)
    {
    list($b4_key, $key, $b4_value, $value) = explode('"', $line);
    if (preg_match("/Lang/", $b4_key))
        { $Lang[$key] = $value; }
    }

########

据我了解,您的文件内容只有一种语言,不是吗?如果没有,将在代码中稍作修改。

<?
// edit the values of an array ($Lang["key"] = "value";) in the file below
$array_file = "lang.php";

// if POSTed, save file
if (isset($_POST['submit'])) {
    unset($_POST['submit']); // remove the submit button from the name/value pairs
    $is_post = true; // set flag for use below

    // write over the original file, and write a backup copy with the date/time
    write_file($array_file, $new_array);
    write_file($array_file . date("_Y-m-d_h-ia"), $new_array);

    file_put_contents($array_file, serialize(array(
        'timestamp' => time(), // after you can display this value in your preffered format
        'data' => serialize($_POST)
    )));
}

$Lang_content = @unserialize(@file_get_contents($array_file));
if (!array_key_exists('data', $Lang_content)) {
    $Lang_content = array(
        'timestamp' => 0, // or time()
        'data' => serialize(array())
    );
}

$Lang_template = array( // If you want
    'TEXT'      => "baer",
    'GRET'      => "hallo",
    'FACE'      => "face",
    'HAPY'      => "happy",
    'TOOL'      => "tool",
);

$Lang = array_merge(
    $Lang_template,
    unserialize($Lang_content['data'])
);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Language Editor</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1><?= $array_file ?></h1>
<? if (!isset($is_post)) { ?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
    <? } ?>
    <table>
        <?
        // loop through the $Lang array and display the Lang value (if POSTed) or a form element
        foreach ($Lang as $key => $value) {
            echo "<tr><td>$key</td><td>";
            echo isset($is_post) ? "= \"$value\"" : "<input type=\"text\" name=\"$key\" value=\"$value\">";
            echo "</td></tr>\n";
        }
        ?>
    </table>
    <? if (!isset($is_post)) { ?>
    <p><input type="submit" name="submit" value="Save"></p>
</form>
<? } ?>
</body>
</html>
于 2013-07-11T05:48:19.880 回答