繁体   English   中英

php字符串替换没有找到一个单词

[英]php string replace without finding a word

我在下面提供了一个表单字段。

`<form method='post'>
    <input type='hidden' name='var'/>
    <input type='hidden' name='en_word' value='HOME'/>
    <input type='text' name='new_word'/>
    <input type='hidden' name='en_word' value='REWARD'/>
    <input type='text' name='new_word'/>
    <input type='hidden' name='en_word' value='LEADERBOARDS'/>
    <input type='text' name='new_word'/>
    <input type='submit'>
 </form>`

当我输入内容并单击提交按钮时,它将执行文件写入功能(fwrite)。如果我输入了第一个输入字段,则单击提交按钮,我将得到“主页”和“无论输入什么”。 现在,我想用新输入的单词(“ Home” =>“НАЧАЛО”)替换НАЧАЛО的translation.php中。我没有НАЧАЛО。 现在,我想替换输入的单词(如果在translation.php中不存在)。

    $var = $_POST['var'];
        $new_words = $_POST['new_words'];
        $en_word = $_POST['en_word'];
        for($i=0; $i<count($var); $i++)
        {
        $file = '/www/translation.php';
        $handle = fopen($file, "r");
        $input = fread($handle, filesize($file));
        $stringData = html_entity_decode($new_words[$i], ENT_COMPAT, 'UTF-8');
        $first_str = "\"$en_word[$i]\""."=>";
        $string="\"$stringData\"".",\n";
        fclose($handle);
        if(!eregi($first_str,$input) && !eregi($string,$input))
        {
            $myFile = "/www/translation.php";
            //echo $en_word[$i];
            $fh = fopen($myFile, 'a') or die("can't open file");
            $stringData = "\"$en_word[$i]\""."=>";
            fwrite($fh, $stringData);
            $stringData = html_entity_decode($new_words[$i], ENT_COMPAT, 'UTF-8');
            fwrite($fh, "\"$stringData\"".",\n");
            fclose($fh);
        }
        elseif (eregi($first_str,$input) && !eregi($string,$input))
        {

// here I want to replace the input word if not exist in translation.php.
        }`
     }

这是translation.php

translation.php包含一个字符串。

"HOME" => "НАЧАЛО",
"REWARDS" => "НАГРАДИ",
"LEADERBOARDS" => "КЛАСАЦИИ",
"LOGIN | SIGN UP" => "ВХОД / РЕГИСТРАЦИЯ",
"STORE" => "МАГАЗИН",
"LOGOUT" => "ИЗХОД",
"SET" => "ПОТВЪРДИ",


现在,我想替换输入的单词(如果在translation.php中不存在)。 我该怎么做呢? 有可能吗? 请帮我。
抱歉! 如果问题无法理解,请告诉我,我会解释清楚。

请将您的html更改为此:

`<form method='post'>
<input type='hidden' name='var'/>
<input type='hidden' name='en_word[]' value='HOME'/>
<input type='text' name='new_word'/>
<input type='hidden' name='en_word[]' value='REWARD'/>
<input type='text' name='new_word'/>
<input type='hidden' name='en_word[]' value='LEADERBOARDS'/>
<input type='text' name='new_word'/>
<input type='submit'>

`

如果没有,则帖子将仅具有最后的en_word值。 请检查它是否现在可以正常使用并恢复正常。

谢谢

因此,您有一个php文件,其中包含某些关键字(例如“ HOME”和“ REWARDS”)的翻译。 然后,您将有一个表格来更改这些关键字的翻译。 在提交时,您想用新的替换旧的翻译。

也许这段代码可以帮助您解决问题。 我首先将整个translation.php加载到$ contents中,然后对它执行preg_replace search&replace。

<?php
// prepare $find and $replace for preg_replace.
$find = array("HOME", "REWARD");
$replace = array("whatever", "foobar");

// convert $find into regular expressions for the whole line
foreach ($find as $i => $key)
    $find[$i] = '~("'.$key.'"\s*=>\s*")[^"]*(",\r?\n?)~u';

// convert $replace to use the first and second part of any match 
// and put the new translation in between 
foreach ($replace as $i => $translation)
    $replace[$i] = '$1'.$translation.'$2';

// read translation.php
$contents = file_get_contents("translation.php");

// do the replacements
$contents = preg_replace($find, $replace, $contents);

// debug output, just delete later
var_dump($find, $replace, $contents);

// you want to save your changes into translation.php
// file_put_contents("translation.php", $contents);
?>

如果您必须在translation.php中管理多个值,请考虑使用mysql之类的数据库来执行此操作。 使用mysql可以更轻松地更改值,甚至具有多种语言支持。

还有一个用于处理文本翻译的特殊库: http : //php.net/manual/de/book.gettext.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM