Announcement

Collapse
No announcement yet.

splitting

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • splitting

    in perl you can open up files and split info up into different variables...i was wondering how do you do it in php?

  • #2
    Code:
    $filename = '/path/to/file.ext';
    $delimiter = '|';
    
    $file = fopen($filename,'r');
    $contents = fread($file,filesize($filename));
    fclose($file);
    
    $data = explode($delimiter,$contents);

    Comment


    • #3
      Or for a regexp:

      Code:
      $filename = '/path/to/file.ext';
      $delimiter = '\|';
      
      $file = fopen($filename,'r');
      $contents = fread($file,filesize($filename));
      fclose($file);
      
      $data = split($delimiter,$contents);

      Comment


      • #4
        then you can do this?
        Code:
        $filename = '/path/to/file.ext';
        $delimiter = '|';
        
        $file = fopen($filename,'r');
        $contents = fread($file,filesize($filename));
        fclose($file);
        
        ($data1,$data2,$data3) = explode($delimiter,$contents);
        for instance, to get 3 vars from the 'split'

        i also am a little hazy on this, just clarifying

        Comment


        • #5
          $data is stored in an array, so for each item you would have $data[0], $data[1], etc

          Code:
          $filename = '/path/to/file.ext';
          $delimiter = '\|';
          
          $file = fopen($filename,'r');
          $contents = fread($file,filesize($filename));
          fclose($file);
          
          $data = split($delimiter,$contents);
          
          echo $data[0];  // First Var
          echo $data[1];  // Second Var
          echo $data[2];  // Third Var

          Comment


          • #6
            <?

            else:

            $delimiter = "\<br><br><br>";

            $file = fopen("$category.html", "r");
            $contents = fread($file,filesize($category.html));
            fclose($file);

            $data = split($delimiter,$contents);

            echo $data[0];
            echo $data[1];
            echo $data[2];


            endif;
            ?>

            nothing outputted from the echo commands even though the file exists

            Comment


            • #7
              This is going to come out bad, but...
              I wouldn't try to adapt a program if you don't know the difference between a variable and a string.

              Code:
              <?php
              
              $delimiter = "<br><br><br>"; //Is it <br><br><br> or \n\n\n?
              $filename = '/path/to/file.ext'; //Full path
              
              //Don't touch below
              
              $file = fopen("$filename", "r"); 
              $contents = fread($file,filesize($filename)); 
              fclose($file); 
              
              $data = split($delimiter,$contents); 
              
              echo $data[0]; 
              echo $data[1]; 
              echo $data[2]; 
              
              ?>

              Comment


              • #8
                it worked thanks

                Comment

                Loading...
                Working...
                X