Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Split pipe delimited string

    What is the equivalent PHP code to split a pipe delimted string?

    In Perl, I would do it like this.
    @array = split(/\|/, $string);

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts

  • Users who have thanked Fumigator for this post:

    tdavis (03-17-2009)

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I am brand new to PHP, so, I am trying a simple script to start off with, using the explode as you suggested; however, I may not be using this in the correct manner.

    Here is my script, which I want to do nothing more than display the contents of a pipe delimited file. When I run it, it seems to get stuck in a loop. Any advice is appreciated!

    <?php
    $file = "groups.txt";
    $file = fopen($file, "r") or exit("Unable to open file!");
    while(!feof($file))
    {
    echo fgets($file). "<br />";
    $array = explode("|", $file);
    echo "<br>$array[0]";
    echo "<br>$array[1]";
    echo "<br>$array[2]";
    echo "<br>$array[3]";
    echo "<br>$array[4]";
    echo "<br>$array[5]";
    echo "<br>$array[6]";
    echo "<br>$array[7]";
    }
    fclose($file);
    ?>

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You are exploding the file handle, not the data retrieved. Instead of echoing the results of fgets(), assign it to a variable and then use that variable in the explode function.

    (though, this wouldn't cause an endless loop I wouldn't think, I'm stumped on that one)

  • #6
    eak
    eak is offline
    Regular Coder eak's Avatar
    Join Date
    Jun 2002
    Location
    Nashville, TN
    Posts
    354
    Thanks
    0
    Thanked 26 Times in 26 Posts
    You could also use fgetcsv to get your data.

    PHP Code:
    $handle fopen("Your.File""r");
    while( (
    $data fgetcsv($handle1000"|") ) !== FALSE ){
        
    print_r$data );
    }
    fclose($handle); 
    eak | "Doing a good deed is like wetting your pants; every one can see the results, but only you can feel the warmth."

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Thanks again!

    Well, not sure about the loop either, but now it works. This is a little more different from Perl than I thought it would be, not that I am a Perl expert.

    Thanks again for your help!

    <?php
    $file = "groups.txt";
    $file = fopen($file, "r") or exit("Unable to open file!");
    while(!feof($file))
    {
    $data = fgets($file);
    // echo fgets($file);
    $array = explode("|", $data);
    echo "<br>$array[0]";
    echo "<br>$array[1]";
    echo "<br>$array[2]";
    echo "<br>$array[3]";
    echo "<br>$array[4]";
    echo "<br>$array[5]";
    echo "<br>$array[6]";
    echo "<br>$array[7]";
    }
    fclose($file);
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •