alkat Posted March 30, 2008 Share Posted March 30, 2008 Hello, sorry to bother you with this very silly question but I'm just getting started with PHP and scripting in general and I can't figure out how to find the answer to all of my doubts... let's start with the first one. I'll be very grateful to anyone who will help. I'm writing (or trying to write) a very simple script which will convert a .strings file into a text file with a couple of custom tags that will help me edit a part of test. Here's a sample .strings file: /* No comment provided by engineer. */ "[man] " = "[man] "; /* No comment provided by engineer. */ "Accessing SIM Card..." = "Reading SIM Info.."; And here's my script: <?php $input = file('l10n.strings'); foreach ($input as $line) { $output = str_replace("/*","<noedit>/*",$line); $output2 = str_replace("\" = \"","\" = \"</noedit>",$output); $output3 = str_replace("\";","<noedit>\";</noedit>",$output2); echo $output3; } ?> As I guess you can easily see, what the script does is convert the above lines into this: <noedit>/* No comment provided by engineer. */ "[man] " = "</noedit>[man] <noedit>";</noedit> <noedit>/* No comment provided by engineer. */ "Accessing SIM Card..." = "</noedit>Reading SIM Info...<noedit>";</noedit> Now, there are a few other things I'd need it to do. The first would be to be able to tell the script which file to convert without me having to specify that into the code. How can I do that? I'd also like to be able to write the output to another file. What I'm doing now is to direct the output to a file using bash like this: $php conversion.php > output.txt I'd also like to be able to upload the file to be converted from a webpage, but I guess I'll have to study some more for that... Or post another question! Thanks for you help. Ale. Quote Link to comment https://forums.phpfreaks.com/topic/98602-noob-how-to-input-a-file-into-a-php-script/ Share on other sites More sharing options...
MadTechie Posted March 30, 2008 Share Posted March 30, 2008 This was written on the fly so probably has a ton of bugs but it should atleast give you a start <?php $dir = "/etc/php5/"; $find["/*"] = "<noedit>/*"; $find["\" = \""] = "\" = \"</noedit>"; $find["\";"] = "<noedit>\";</noedit>"; // Open a known directory, and proceed to read its contents if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($filename = readdir($dh)) !== false) { //if($filename != "." || $filename != "..") //check its a file //removed if(preg_match('/\.strings$/sm', $filename )) //check its ends with .strings { $file = dir."/".$filename; $str = file_get_contents($file); //$output = str_replace("/*","<noedit>/*",$line); //$output2 = str_replace("\","\" = \"</noedit>",$output); //$output3 = str_replace("\";","<noedit>\";</noedit>",$output2); $str = str_replace(array_keys($find), array_values($find), $str ); file_put_contents($file.".NEW",$str); //remove the .".NEW" after testing } } closedir($dh); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98602-noob-how-to-input-a-file-into-a-php-script/#findComment-504652 Share on other sites More sharing options...
alkat Posted March 30, 2008 Author Share Posted March 30, 2008 Thanks MadTechie, there was a missing $ but the script works very well. However, I still don't understand what I shall do to make it look for a specific file or, in the case of your script, directory. I mean, I can write the directory into the code and always put the files in there, which is good enough for me, but I'd like to also be able to have the script ask me the path for the directory which contains the file. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/98602-noob-how-to-input-a-file-into-a-php-script/#findComment-504700 Share on other sites More sharing options...
MadTechie Posted March 30, 2008 Share Posted March 30, 2008 you could create a form, in which you could post the path! then have $dir = $_POST['path']; Quote Link to comment https://forums.phpfreaks.com/topic/98602-noob-how-to-input-a-file-into-a-php-script/#findComment-504707 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.