简体   繁体   English

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

[英]php string replace without finding a word

I have a form field its give below. 我在下面提供了一个表单字段。

`<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>`

When I entered something and click the submit button it will perform a file write function(fwrite).if i entered the first input field then i click submit button i will get "home" and "whatever i entered". 当我输入内容并单击提交按钮时,它将执行文件写入功能(fwrite)。如果我输入了第一个输入字段,则单击提交按钮,我将得到“主页”和“无论输入什么”。 now i want to replace in translation.php for НАЧАЛО with new entered word("Home"=>"НАЧАЛО").I doesn't have НАЧАЛО. 现在,我想用新输入的单词(“ Home” =>“НАЧАЛО”)替换НАЧАЛО的translation.php中。我没有НАЧАЛО。 Now I want to replace the input word if not exist in 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.
        }`
     }

Here is the translation.php 这是translation.php

translation.php contain a string. translation.php包含一个字符串。

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


Now I want to replace the input word if not exist in translation.php. 现在,我想替换输入的单词(如果在translation.php中不存在)。 how do I do this? 我该怎么做呢? Is It possible to do? 有可能吗? please help me. 请帮我。
Sorry! 抱歉! if question is not understandable please tell me I will explain clearly. 如果问题无法理解,请告诉我,我会解释清楚。

Please change your html to this: 请将您的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'>

` `

If not, the post will only havethe last en_word value. 如果没有,则帖子将仅具有最后的en_word值。 Please check if it works now and comeback. 请检查它是否现在可以正常使用并恢复正常。

Thanks 谢谢

So you have a php-file that includes translations for certain keywords like "HOME" and "REWARDS". 因此,您有一个php文件,其中包含某些关键字(例如“ HOME”和“ REWARDS”)的翻译。 you then have a form to change the translation for those keywords. 然后,您将有一个表格来更改这些关键字的翻译。 on submit you want to replace the old translations with new ones. 在提交时,您想用新的替换旧的翻译。

Maybe this code can help you solve your problem. 也许这段代码可以帮助您解决问题。 i first load the whole translation.php into $contents, then perform a preg_replace search&replace on it. 我首先将整个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);
?>

if you have to manage more than a few values in your translation.php, consider using a database like mysql to do this. 如果您必须在translation.php中管理多个值,请考虑使用mysql之类的数据库来执行此操作。 changing the values and even having multiple language support is way easier with mysql. 使用mysql可以更轻松地更改值,甚至具有多种语言支持。

there also is a special library for handling translations of text: http://php.net/manual/de/book.gettext.php 还有一个用于处理文本翻译的特殊库: http : //php.net/manual/de/book.gettext.php

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

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