Chrazy Posted August 27, 2008 Share Posted August 27, 2008 Background: Multilingual site. The site will have an admin gui where the admins can edit the existing files that are used. Each language has its own file (eg lang_en.php) where x numbers of variables exists with their string content. All the variables are namned the same for all the languages but where the content differs depending on the language. For the admin the content of the file is printed when the line starts with a "$" to make it so that just the lines containing actual information will be printed. The file itself is read in using file(), split into two entities with explode() to be able to keep track of the variable name and its content when the actual content is the only thing that is relevant to show. I've noticed that I had to add some trim functions to make it work. My problem lies in that the code I've written isn't working to 100%. "Usually" there is nothing wrong when you just add some new content to a variable (although I've recently found that this doesn't work to 100% each time either). But when you remove something from an existing string it seems as if the same amount of characters removed are added to the end of the file, the characters being added are drawn from the last portion of the file. E.g. <?php $var¤ = ”Hey hey”; $var2¤ = ”Hello hello”; ?> If the last "hey" is removed from $var¤ the file looks something like this afterwards <?php $var¤ = ”Hey”; $var2¤ = ”Hello hello”; ?> ”;?> The variable content itself is saved as it's supposed toexcept for the part that is added to the end of the file. Not sure if I have submitted enough information, holler if I need to provide more... The code is a selection of what I thought was the relevant parts. :? $thefile = "lang/lang_". $_SESSION['slang'] .".php"; $fh = fopen($thefile, "r+"); $sel_file = file($thefile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $al = count($sel_file); // $al = array length for($i = 1; $i <= $al; $i++) //print the lines { $strang = $sel_file[$i-1]; //line in the array if(substr($strang,0,1) == '$') { $text = explode('¤', $strang); //dela upp string vid "¤" // variabel : $text[0] - texten : $text[1] $text[1] = trim($text[1]); //trim $text[1] = ltrim($text[1], '='); //trim $text[1] = trim($text[1]); //trim $text[1] = ltrim($text[1], '"'); //trim $text[1] = rtrim($text[1], '";'); //trim $varname[$i-1] = trim($text[0]); //trim echo '<TEXTAREA NAME="'. $varname[$i-1] .'" ROWS="1" COLS="50">'. $text[1] .'</TEXTAREA>'; echo '<br /><br />'; } } if(isset($_POST['submit'])) { $nytext = ""; for($j = 1; $j <= $al; $j++) // { $temptext = $_POST[$varname[$j-1]]; $temptext = trim($temptext); $varname[$j-1] = trim($varname[$j-1]); //trim if(substr($varname[$j-1],0,1) == '$') { $nyrad = $varname[$j-1].'¤ = "'. $temptext .'";'."\n"; //put the variable back together with the new text and add the previously removed characters } else { $nyrad = $sel_file[$j-1]."\n"; } if($nytext != "") { $nytext = $nytext . $nyrad; } else { $nytext = $nyrad; } } $nytext = rtrim($nytext, "\r\n") . PHP_EOL; fwrite($fh, $nytext); fclose($fh); fclose($fp); } Link to comment https://forums.phpfreaks.com/topic/121526-editing-files/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.