[英]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.