简体   繁体   中英

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". now i want to replace in translation.php for НАЧАЛО with new entered word("Home"=>"НАЧАЛО").I doesn't have НАЧАЛО. Now I want to replace the input word if not exist in 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 contain a string.

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


Now I want to replace the input word if not exist in 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:

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

<?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. changing the values and even having multiple language support is way easier with mysql.

there also is a special library for handling translations of text: http://php.net/manual/de/book.gettext.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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