PHP语言编辑器,数组键

I have found the script. http://www.aota.net/forums/showthread.php?t=24155

My lang.php

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

My 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])."\",
"; }

    // 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>
";
    }
    ?>
</table>
<? if (!isset($is_post)) { ?>
<p><input type="submit" name="submit" value="Save"></p>
</form>
<? } ?>
</body>
</html>

Unfortunately I can not read array and Keys from lang.php.

Script writer, writes: all lines in array.txt did do set $ Bid [] will be lost.

But I want lang.php after the change save as a PHP file that would go?

I want to create a drop-down list and load array with matching keys, change key text and save.

I thank you in advance for your help!

You shouldn't read lang.php file. You should include it.

Better way:

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

and remove these lines:

// 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; }
    }

# # # # # # # #

As I understand your file contents only one language, doesn't it? If no, will be a little modifications in the code.

<?
// 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>
";
        }
        ?>
    </table>
    <? if (!isset($is_post)) { ?>
    <p><input type="submit" name="submit" value="Save"></p>
</form>
<? } ?>
</body>
</html>