editing notepad text file through php. need to remove lines.

Discussion in 'PHP' started by hansab, Oct 9, 2011.

  1. #1
    Hi Friends,

    I really need your help. I have a huge notepad file. And i need to remove few things from the notepad.

    Every line in notepad file is like this

    Game|is|On|Off|One|World
    World|Internet|Cafe|Computer|Windows

    So this is the sample.

    What i want is, that to code a php script which can just do this


    count the character | and as soon as it reaches the 5th "|" it should remove all the other parts of the line.

    So if the line before is like

    Game|Cricket|Football|Soccer|Mother|Father|Game|Daddy|Computer

    After running the script

    The line will be
    Game|Cricket|Football|Soccer|Mother

    I need a loop so that all the lines in the text files are edited this way. Please help me if you can. I know it will take a bit time but it is really for a good cause. It is really going to help me save days of time and complete my assignment report. Please help.

    Thanks
     
    hansab, Oct 9, 2011 IP
  2. hansab

    hansab Active Member

    Messages:
    162
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Some lines dont have more words so only if it goes to 5th, it should happen. If it has less then next line.

    PLEASE PLEASE PLEASE SOMEONE HELP ME :(
     
    hansab, Oct 9, 2011 IP
  3. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #3
    This is very easy to complete in PHP all you need to do is explode all data into an array the loop through the results and do a str_replace to take out | when the loop has increased by 5.

    Google: php explode and str_replace

    Thanks
     
    HuggyEssex, Oct 9, 2011 IP
  4. sezero

    sezero Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    not sure if this is what you were really asking for but..

    
    <?
    $filename = "files/filename.txt";
    $filename_save_as = "files/filename_saved.txt";
    $words_per_line = 5;
    /* ==================================================== */
    $file_pointer = fopen($filename, "rb");
    if($file_pointer!=false) {
        while(!feof($file_pointer)) {
            $data = fgets($file_pointer);
            $explode = explode("|", $data);
            if(count($explode) > $words_per_line) { 
                for ($i = 0; $i < $words_per_line; $i++) {
                    $output .= str_replace("\n", "", $explode[$i]);
                    if($i != $words_per_line-1) { $output .= "|"; }
                }
                $output .= "\n";
            }else{
                $output .= str_replace("\n", "", $data)."\n";
            }
        }
    }
    $file_save = fopen($filename_save_as, 'w');
    fwrite($file_save, $output);
    fclose($file_save);
    echo "file complete, saved as ".$filename_save_as;
    /* ==================================================== */
    ?>
    
    PHP:
    upload that somewhere and create a directory called "files", make sure you chmod that directory 0777
     
    sezero, Oct 9, 2011 IP
  5. hansab

    hansab Active Member

    Messages:
    162
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Thanks, i havent tried it yet but you are the one who gave me the code instead of suggesting me to search in google etc. If i was a programmer, i would have searched the net. I needed the code so i could copy paste. And you did that. Thanks. I will try it.

     
    hansab, Oct 12, 2011 IP
  6. sezero

    sezero Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    no problem, good luck
     
    sezero, Oct 13, 2011 IP