【问题标题】:PHP Language Editor, Array KeysPHP 语言编辑器,数组键
【发布时间】:2013-07-11 05:42:07
【问题描述】:

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

我的 lang.php

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

我的编辑.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 文件,这样可以吗?

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

提前感谢您的帮助!

【问题讨论】:

  • 再一次 - “我找到了脚本,但它不起作用”...您尝试过什么让它起作用?你得到什么输出?你想得到什么?

标签: php arrays key


【解决方案1】:

您不应该阅读 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>

【讨论】:

  • 但对我来说 lang.php 不是一个好主意。只需序列化语言数组并保存到某个文件(不是 php)。在您可以读取和反序列化之后,将获得相同的数组。我认为这是更好的方法。
  • 您好 Vitaly,感谢您的提示是否有效。但我想将 lang.php 保存为 PHP,这样可以吗?现在,保存后会删除其他所有内容。 :-(
  • 我刚刚读到这非常令人兴奋,我很想通过序列化、反序列化来做到这一点。你能帮我写一些代码来帮助我吗?
  • 读取:$Lang = unserialize(@file_get_contents('LANG_FILE'));写:file_put_contents('LANG_FILE', serialize($Lang));
  • 为了改进这一点,您还可以使用模板 Lang 数组: $Lang_template = array(/* TEMPLATE VALUES */); $Lang = array_merge($Lang_template, unserialize(@file_get_contents('LANG_FILE')));如果 lang 文件已损坏(或不存在)或没有某些变量,则将使用模板值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多