Results 1 to 2 of 2

Thread: Reading strings from file

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Reading strings from file

    Ok, what I am trying to do is read a set of values from a file. For example:

    1=stuff
    2=more stuff

    Now, this is the code I am using to get the values:
    PHP Code:
    <?
    $pFile = fopen ("data.txt","r");

    while ($info = fscanf($pFile, "%d=%s\n"))
    {
        list($first, $second) = $info;
        echo $first . ": " . $second . "<br>";
    }
    fclose($pFile);
    ?>
    However, the problem is, it only reads up to the whitespace, so I get "more" for the second line instead of "more stuff." How do I take in spaces too?
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'm probably going to throw you off here, but I'd do this:

    PHP Code:
    $contents file("data.txt");

    for (
    $i 0$i count($contents); $i++) {
        
    $parts explode("="$contents[$i]);
        echo 
    $parts[0] . ":" $parts[1] . "<br>";

    Of course that'd only work if all the lines in your file are #=something.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width