Jump to content

[NOOB] How to input a file into a php script


alkat

Recommended Posts

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.

 

Link to comment
Share on other sites

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);
    }
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.