File corrupt, need a unique find/replace capability.

Discussion in 'Programming' started by JosS, Feb 11, 2007.

  1. #1
    I have files, with hundreds of lines of code written in HTML/PHP and wherever there used to be a line break,

    Example

    Line 1
    Line 2

    The files have turned into one line, and are set out like this

    Line1\nLine2\nLine3\n

    I need to convert them back somehow. Any help would be greatly appreciated.
     
    JosS, Feb 11, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    foreach( scandir(".") as $file )
    {
    $old = file_get_contents( $file );
    file_put_contents( $file, str_replace( "\n", "\r\n", $old ) );
    }
    
    PHP:
    server will need write permissions on the folder / files while you execute that code .....
     
    krakjoe, Feb 11, 2007 IP
  3. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    foreach( scandir(".") as $file )
    {
    $old = file_get_contents( $file );
    file_put_contents( $file, str_replace( "\n", "\r\n", $old ) );
    }
    ?>

    I made a file called yo.php then put a few .txt documents with all the code to be fixed, set 777 permissions

    but get this error when executing yo.php

    Fatal error: Call to undefined function: scandir() in /home/server/public_html/fixsites/yo.php on line 2
     
    JosS, Feb 11, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    missing a } ?

    
    <?php
    foreach( scandir(dirname(__FILE__)) as $file )
    {
    if($file != "." && $file != ".."):
      $old = file_get_contents( $file );
      file_put_contents( $file, str_replace( "\n", "\r\n", $old ) );
    endif;
    }
    ?>
    
    PHP:
    Still might need different code, let me know what happens and this time if it says theres an error, post the line mentioned.
     
    krakjoe, Feb 11, 2007 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    TwistMyArm, Feb 11, 2007 IP
  6. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I've tried changing where it says scandir to opendir but I still get the error.
     
    JosS, Feb 11, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    Yeah it's specifically php5, which is why I said you might need different code :

    
    if ($handle = opendir('.')) {
       while (false !== ($file = readdir($handle))) {
           if ($file != "." && $file != "..") {
               $old = file_get_contents( $file );  
               file_put_contents( $file, str_replace( "\n", "\r\n", $old ) );
           }
       }
       closedir($handle);
    }
    
    PHP:
     
    krakjoe, Feb 11, 2007 IP
  8. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Fatal error: Call to undefined function: file_put_contents() in /home/server/public_html/fixsites/yo.php on line 6
     
    JosS, Feb 11, 2007 IP
  9. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I think I need to use fopen, fwrite and fclose to accomplish this.

    Any ideas?
     
    JosS, Feb 12, 2007 IP
  10. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #10
    
    <?php
    if(!function_exists("file_put_contents") )
    {
      function file_put_contents( $file, $data )
      {
        $handle = fopen($file, "w+");
        if(!$handle) : return false; endif;
        if(!fwrite( $handle, $data, strlen($data))): return false; endif;
        fclose($handle);
        return true;
      }
    }
    if ($handle = opendir('.')) 
    {
       while (false !== ($file = readdir($handle))) 
       {
           if ($file != "." && $file != "..") 
           {
               $old = file_get_contents( $file );  
               file_put_contents( $file, str_replace( "\n", "\r\n", $old ) );
           }
       }
       closedir($handle);
    }
    ?>
    
    PHP:
    ???
     
    krakjoe, Feb 12, 2007 IP
  11. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I installed PHP on my computer, so I could run these snippets, and all of them dump an empty file.

    I have it set up like this

    fix.php
    fix\


    inside fix\ is a file called test.txt and in text.txt is jumbled one line code, with \n where there used to be line drops.

    When I run fix.php, it dumps a file like this

    fix.php
    fix\
    test.txt

    and inside test.txt theres NOTHING.

    Almost there! Thanks heaps for your help so far too!
     
    JosS, Feb 13, 2007 IP
  12. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #12
    code needs to be in the same folder as the files being modified.

    Also, wouldn't bother writing \n in any files to test, download a select few from your site to see what happens.

    Failing that, send me a zip with a few of these files in so I can have a looksee for myself .....
     
    krakjoe, Feb 13, 2007 IP
  13. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #13
    ok, i've tried that aswell, having all in the same folder.

    The code looks like this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" ><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >\n<head>\n<title><? echo $title ?></title>\n<?php \nif ( $top == ""){\n $top = "

    but it just keeps on going and going in one line. See all the \n 's ? They should be line drops
     
    JosS, Feb 13, 2007 IP
  14. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #14
    like I sed zip up some broken text files and pm them to me ....
     
    krakjoe, Feb 13, 2007 IP
  15. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #15
    ok it NEEDS to be in the same folder as the files, and do not chmod this file to 777 only the html files.

    
    <?php
    if(!function_exists("file_put_contents") )
    {
      function file_put_contents( $file, $data )
      {
        $handle = fopen($file, "w+");
        if(!$handle) : return false; endif;
        if(!fwrite( $handle, $data, strlen($data))): return false; endif;
        fclose($handle);
        return true;
      }
    }
    if ($handle = opendir('.'))
    {
       while (false !== ($file = readdir($handle)))
       {
           if ($file != "." && $file != "..")
           {
               $old = file_get_contents( $file ); 
               file_put_contents( $file, str_replace( '\n', "\n", $old ) );
           }
       }
       closedir($handle);
    }
    ?>
    
    PHP:
    It works, I tested it .....
     
    krakjoe, Feb 13, 2007 IP
  16. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #16
    JosS: you can't just type '\n' into a text editor and expect it to work... '\n' is a special character that you can't just type. It's a control character: if you can see the characters '\' and 'n', then that is not the same as a '\n' character.
     
    TwistMyArm, Feb 13, 2007 IP
  17. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Yeah, I ended up figuring out that, I had to do a normal search and replace in notepad to replace the \n's to something like poop, then use the PHP script to replace poop to \r\n.

    If that made sense.
     
    JosS, Feb 14, 2007 IP