Jump to content

Recommended Posts

Hello,

last week some guy here helped me write a script but now I have another issue for which I need helping.

 

There's something wrong with the following script and how it writes text to the beginning of a file:

 

<?php
$dir = "./files/";

/*$find["/*"] = "<noedit>/*";*/
$find["\" = \""] = "\" = \"</NOEDIT><TRAD>";
$find["\"=\""] = "\"=\"</NOEDIT><TRAD>";
$find["\";"] = "</TRAD><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(preg_match('/\.strings$/sm', $filename )) // check if the file ends with .strings
            {
               $file = $dir."/".$filename;
               $str = file_get_contents($file); // reads entire file into a string
               $str = str_replace(array_keys($find), array_values($find), $str ); // 
               file_put_contents($file.".html",$str); // adds .html to the filename
       // opens file to write to it
               $handle1 = fopen($file.".html", 'r+'); 
               $opentags = "<OPEN><NOEDIT>\n"; 
               fwrite($handle1, $opentags); // adds opening tags to the beginning of the file
       // opens file to write to it
               $handle2 = fopen($file.".html", 'a'); 
               $closetags = "</NOEDIT></OPEN>"; 
               fwrite($handle2, $closetags); // adds closing tags to the end of the file
            }
        }
        closedir($dh);
    }
}
?>

 

When writing to the beginning of the file, it deletes part of the first line of the original file. Here's the original:

 

/* Tue Apr 01 14:55:39 +0200 2008 --- file: pl-Program.strings --- encoding utf-16 be */

/* begin: $SRCROOT//Option/GlobeTrotter Connect/Resources/pl.lproj/Localizable.strings */

"Change network mode error" = "Change network mode error";

 

And the same file after running my script against it:

 

<OPEN><NOEDIT>
r 01 14:55:39 +0200 2008 --- file: pl-Program.strings --- encoding utf-16 be */

/* begin: $SRCROOT//Option/GlobeTrotter Connect/Resources/pl.lproj/Localizable.strings */

"Change network mode error" = "</NOEDIT><TRAD>Change network mode error</TRAD><NOEDIT>";

 

As you can see, the first part of the text is cut off. The same does not happen with the end of the file.

 

Can anyone shed some light on this issue? :)

 

Thanks,

Ale.

Link to comment
https://forums.phpfreaks.com/topic/99856-write-to-beginning-of-a-file/
Share on other sites

try:

<?php

$dir = "./files/";

/*$find["/*"] = "<noedit>/*";*/
$find["\" = \""] = "\" = \"</NOEDIT><TRAD>";
$find["\"=\""] = "\"=\"</NOEDIT><TRAD>";
$find["\";"] = "</TRAD><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(preg_match('/\.strings$/sm', $filename )) // check if the file ends with .strings
            {
                $file = $dir . '/' . $filename;

                $str = file_get_contents($file); // reads entire file into a string
                $str = str_replace(array_keys($find), array_values($find), $str );

                $str = "<OPEN><NOEDIT>\n" . $str . "\n</NOEDIT></OPEN>";

               file_put_contents($file.'.html', $str); // adds .html to the filename
            }
        }

        closedir($dh);
    }
}

?>

 

I remove the need to have to following lines:

	       // opens file to write to it
               $handle1 = fopen($file.".html", 'r+'); 
               $opentags = "<OPEN><NOEDIT>\n"; 
               fwrite($handle1, $opentags); // adds opening tags to the beginning of the file
       // opens file to write to it
               $handle2 = fopen($file.".html", 'a'); 
               $closetags = "</NOEDIT></OPEN>"; 
               fwrite($handle2, $closetags); // adds closing tags to the end of the file

 

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.