I have a file that has 3 columns separated by 4 spaces. I want to have these four spaces replaced with one ;(semicolon). I've tried several different ways of doing this and have failed. Here is my script:

$filename = 'tidlist.txt';

$open = fopen($filename, 'r+');

function replaceText($text) {
$text = str_replace(" ", ";", $text);
}

fwrite($open, $text);

fclose($open);

Here is a snipet of the file(tidlist.txt) I'm trying open and write to:

192.168.207.209 T1125 192.168.207.209
192.168.105.174 T1126 192.168.105.174
192.168.105.175 T1127 192.168.105.175
192.168.105.173 T1128 192.168.105.173
192.168.242.207 T1129 192.168.242.207
192.168.051.210 T1130 192.168.51.210
192.168.053.084 T1131 192.168.53.84
192.168.128.178 T1132 192.168.128.178

supposed to be 4 spaces between the columns above. Thanks

    ok you prolly want to use the fopen mode "w+" to

    Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

    you need to read the info from the file into string before your can run your replacetext() function on it.

    also your not even calling the function.

    somthing like this should work.

    function replaceText($text) {
    $text = str_replace(" ", ";", $text);
    }
    
    $filename = 'tidlist.txt';
    
    $open = fopen($filename, 'w+');
    $content = fread($open,filesize($filename));
    
    replacetext($content);
    
    fwrite($open,$text);
    
    fclose($open);
    

      now my file tidlist.txt is blanked out...

      if I do r+, it does nothing to the file, and if I use w+, the file is empty.

      Also, I need to replace 4 spaces with one semicolon.

      Thanks for your help.

        this is what i understand you want to do...

        1. read the file get all the infomation into a string or array (which ever you prefer.)

        2.do a str_replace on your string/array of infomation

        3.overwrite all the infomation in the old file with the new info that you have made

        is this what you wanted to do?

        replace

        192.168.207.209 T1125 192.168.207.209

        with

        ;192.168.207.209;T1125;192.168.207.209; (or somthing like that)

          TheJag wrote:

          if I do r+, it does nothing to the file, and if I use w+, the file is empty.

          this could mean that your ReplaceText() function is not outputing anything ($text is empty) try make it global
          as in..

          function replaceText($text) {
          global $text;
          $text = str_replace(" ", ";", $text);
          } 
          

            same thing. 🙁

            Here's the code as I have it now:

            function replaceText($text) {
            global $text;
            $text = str_replace("    ", ";", $text);         
            } $filename = 'tidlist.txt'; $open = fopen($filename, 'w+'); $content = fread($open,filesize($filename)); replacetext($content); fwrite($open,$text); fclose($open);

              and yes, you have it right.

              I want to replace the 4 spaces with a semicolon. I don't want the semicolon at the beginning.

                TheJag wrote:

                same thing. 🙁

                Here's the code as I have it now:

                function replaceText($text) {
                global $text;
                $text = str_replace("    ", ";", $text);         
                } $filename = 'tidlist.txt'; $open = fopen($filename, 'w+'); $content = fread($open,filesize($filename)); replacetext($content); fwrite($open,$text); fclose($open);
                $text = str_replace("    ", ";", $text); 

                thats four spaces.. there arent any occuurances of 4 spaces in the string. do one space

                heres is an overall easier way of doing it..

                <?
                $filename = 'tidlist.txt';
                $lines = file($filename);
                
                foreach($lines as $line) {$new_lines .= str_replace(' ',';',$line)."\n";}
                
                $handle = fopen($filename,"w");
                fwrite($handle,$new_lines);
                fclose($handle);
                ?>
                

                  Well that worked. Thanks for the help. One more question. I don't understand what the . before = sign means. That's one reason I didn't go your route. Didn't know what that meant.

                    Write a Reply...