What Function Should Be Used?

Discussion in 'Programming/Scripts' started by GoldWyvern, Aug 6, 2008.

  1. GoldWyvern

    GoldWyvern New Member

    I'm learning PHP day by day, and have created a few useful (to me) scripts all by myself without modifying someone else's code.

    But I'm stumped. You see, I want a script to do a certain thing but I have no idea how to go about it.

    I don't want people to write a script for me, I want to write it myself, so I will not be too specific. (but if you have example script I can examine and figure out, that would probably be easiest for me to grasp)

    What I have, by example, is a text file. In the text is scrambled data.

    1. The text file has repeating and/or non-consistent data. Matching words will not work. (preg_match)

    2. The text file however, is always the same in that what I want is always on the same line, and is always only numbers.

    3. I want to take the information from that line, but the numbers only (will be together) and put them in a variable.

    4. The output would not have to be an array. It will be one block of numbers, say '58142'.

    5. I would have to do it multiple times in the file, there are several lines I want to pull numbers from, BUT I do not want to do anything WITH it yet. I just want each instance in a separate variable (once I know how to do one line, I'm sure I could figure out how to do that)

    What do I need then? Figuring out what function or set of functions would do this, and how that particular function works (an example, in other words).

    Two weeks searching and I can't find one.

    I'm only at the level of writing to text files right now, I don't have MYSQL experience. However, if MYSQL is best, mention it and how to input it into a database/table in MYSQL, and I will try it that way! :)

    Thanks a lot. Even where to start LOOKING would be appreciated. (and don't say php.net, LOL - already looked there)
     
  2. GoldWyvern

    GoldWyvern New Member

    I just looked at some more information, but not some that would help me in any way. But a question might come up - what is the size of the file in question?

    Small. VERY small. 20 lines or less, and only containing a few words and one number block per.

    There is no way to assign them to database keys in MYSQL because the words in front of the numbers change randomly.

    i.e

    I am a line
    Random Word
    List One 5675765
    Blue Fox 66611 Test
    8104
    Red Blue Green 3 Red
    0 Blue

    etc.

    I just want to know the NUMBER on say, line 3 and stick it into a variable called $linethree or similar.
     
  3. burschik

    burschik New Member

    Since the file is so short, you can use the "file" function to read it into an array.
     
  4. GoldWyvern

    GoldWyvern New Member

    First, thank you for replying.

    I see what you're getting at here. If the file is taken in as an array, I could call separate parts of the file, right? But I can't possibly know what part of file/line the number will be I need to extract. Unless I'm wrong?
     
  5. GoldWyvern

    GoldWyvern New Member

    Here is the output of my array.

    Array ( [0] => Sleepy Willow [1] => Landgrave Fallen Leaves Turns: 101 19:34:32 EDT - 08.06.08 [2] => Number: [3] => Faith: [4] => Race: [5] => Rank: [6] => Guild: [7] => Turns Used: 10006 [8] => Angelique [9] => Dwarven [10] => 85 [11] => DEM [12] => 989 Land: [13] => Serfs: [14] => Income: [15] => Expenses: [16] => Worth: [17] => Chest: 8,021 [18] => 28,771 [19] => 309,480 [20] => 347,576 [21] => 209,473 [22] => 1,091,973 Stonefoots: [23] => Ironfists: [24] => Blacklocks: [25] => Strongholds: [26] => Firebeards: [27] => Longbeards: 5,486 E [28] => 0 G [29] => 1,355 E [30] => 178 E [31] => 0 G [32] => 4,923 E )

    Now, I could take each WORD and output each into an array value.. but then, next time a user inputs it could actually be two words where there was once one or vice versa.. and the keys would change.

    So that's out.

    What I want to do, is keep this at 32 keys, but strip out the words. Leave only the numbers. That way, I can compare and say, '$expenses=(whatever the array key is)'.
     
  6. GoldWyvern

    GoldWyvern New Member

    Ok, let me tell you exactly what I'm doing, so it's clearesr.

    I am learning PHP, and in that, I am creating projects for myself that are actually useful. (that way I don't feel like I'm just doing 'examples')

    I play a game called Canon. It's a text based game. On 'log in', you get presented with your stats. You can copy and paste this (the info above) but as you can see, it doesn't come out clearly. There are several races and several types of kingdoms, so the info changes, but it's always exactly the same amount of lines.

    Now, I figure what I can do is extract the numbers, and then figure out what line contains what number for what stat - and then manipulate that data.

    That's really what I'm trying to get at - that point, of having the info available, so I could then go and try various things. Learn from this data, maybe try to write a comparison script, etc.
     
  7. falko

    falko Super Moderator Howtoforge Staff

    PHP:
    <?php
    $contents 
    file_get_contents('/path/to/file');
    $lines explode("\n"$contents);
    foreach(
    $lines as $line){
      
    $words explode(" "$line);
      foreach(
    $words as $word){
       if(
    is_number($word)) echo $word;
      }
    }
    ?>
     

Share This Page