split -command ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nieuws

    split -command ??

    Hi,

    I was trying to do the following. It's my first php "project", so it's quiet
    logic that i have some problems. Perhaps the php community might help. It's
    about this :

    I have a txt file with the following data :

    1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich 2.37; 4. John
    Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12;

    It's a ranking of a race. Now what i want to reach is, that these names,
    come into an array, eventually in an sql database. But the last thing, is
    not this big problem, if I succeed in getting it into an array.

    So the result might look something like this

    Stijn Piot
    Kim Van Rooy
    ....

    It would be even cooler, if in front of these names was

    1 | Stijn Piot | 58.12
    2 | Kim Van Rooy | 1.28
    3 | .... | 2.54
    ....
    10| John Terlaeken | 1 ronde/tour/round behind

    Now the last thing, I didn't tried, because it seems to complicated for my
    first "project". The first however must be possible, and it can't be that
    difficult. This is what I had, but it didn't worked. Can somebody help me to
    reach the exact result. Thankx in advance

    <? php
    $textfile = "1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich
    2.37; 4. John Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12;"
    // this is a part of the textfile

    $position=split ("\;", $textfile); //splitting in an array with the
    semicolon, the result is an array with rows like this 1. stijn piot 58.12
    $max = count($position ); // counting the size of the array

    $i =0;
    For ($i=0; $i<$max; $i++) {
    $time = split ("^[0-9]+\.[[:space:]]", $position[$i]); // the row 1.
    stijn piot 58.12 is splitted according "number space", the result is stijn
    piot 58.12
    $name = split ("[0-9]+\.[0-9]+", $time[1]);
    echo $name[0]; // the previous result stijn piot 58.12 is splitted
    according "number.number" , the final result should be stijn piot
    }
    // because it is a for loop, eventually i should have
    //stijn piot
    //kim van rooy
    //.... but the farest i can get is just stijn piot and further nothing.
    ?>

    It's not easy at all, i hope the community can do something for me. I'm sure
    that this script can help quiet a lot of people. ;)

    Ciao

    Stijn


  • Mark Hewitt

    #2
    Re: split -command ??


    "nieuws" <stijnthe1st@ya hoo.com> wrote in message
    news:Lao0b.7495 8$F92.8325@afro dite.telenet-ops.be...[color=blue]
    > Hi,
    >
    > I was trying to do the following. It's my first php "project", so it's[/color]
    quiet[color=blue]
    > logic that i have some problems. Perhaps the php community might help.[/color]
    It's[color=blue]
    > about this :
    >
    > I have a txt file with the following data :
    >
    > 1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich 2.37; 4.[/color]
    John[color=blue]
    > Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12;
    >
    > It's a ranking of a race. Now what i want to reach is, that these names,
    > come into an array, eventually in an sql database. But the last thing, is
    > not this big problem, if I succeed in getting it into an array.
    >
    > So the result might look something like this
    >
    > Stijn Piot
    > Kim Van Rooy
    > ...
    >
    > It would be even cooler, if in front of these names was
    >
    > 1 | Stijn Piot | 58.12
    > 2 | Kim Van Rooy | 1.28
    > 3 | .... | 2.54
    > ...
    > 10| John Terlaeken | 1 ronde/tour/round behind
    >[/color]

    If I had a choice on how the data file would be strcutured, I would go for
    the delimited versions
    indicated above. I don't like having to parse files with a free structure,
    it just makes the code
    uneccessarily complex.

    I would structure my file as:

    1|Stijn Piot |58.12
    2|Kim Van Rooy|1.28
    3| .... |2.54

    The use explode() to split the file contents, instead of split which has the
    overhead of having to
    process the delimiter as a regular expression.

    Thus:

    $race_results = file("path_to_t ext_file");

    foreach ( $race_results as $result_line )
    {
    $participant = $explode('|',$r esult_line);
    $sql = "insert into your_table (postion, name, time)
    values('$partci pant[0]','$participant[1]','$participant[2]')";

    mysql_query($sq l); // IRL we would andle mysql errors here as
    well, omitted for clarity
    }

    Somthing along those lines, see how simple it makes the code?
    You might want to add an extra step and trim whitespace off the elements
    resulting from the explode.

    Thanks,
    Mark
    ---------------------------------------------------------------------------
    Windows, Linux and Internet Development Consultant
    Email: corporate@scrip tsmiths.com
    Web: http://www.scriptsmiths.com
    ---------------------------------------------------------------------------

    [color=blue]
    > Now the last thing, I didn't tried, because it seems to complicated for my
    > first "project". The first however must be possible, and it can't be that
    > difficult. This is what I had, but it didn't worked. Can somebody help me[/color]
    to[color=blue]
    > reach the exact result. Thankx in advance
    >
    > <? php
    > $textfile = "1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny[/color]
    Marcovich[color=blue]
    > 2.37; 4. John Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12;"
    > // this is a part of the textfile
    >
    > $position=split ("\;", $textfile); //splitting in an array with the
    > semicolon, the result is an array with rows like this 1. stijn piot 58.12
    > $max = count($position ); // counting the size of the array
    >
    > $i =0;
    > For ($i=0; $i<$max; $i++) {
    > $time = split ("^[0-9]+\.[[:space:]]", $position[$i]); // the row 1.
    > stijn piot 58.12 is splitted according "number space", the result is stijn
    > piot 58.12
    > $name = split ("[0-9]+\.[0-9]+", $time[1]);
    > echo $name[0]; // the previous result stijn piot 58.12 is splitted
    > according "number.number" , the final result should be stijn piot
    > }
    > // because it is a for loop, eventually i should have
    > //stijn piot
    > //kim van rooy
    > //.... but the farest i can get is just stijn piot and further nothing.
    > ?>
    >
    > It's not easy at all, i hope the community can do something for me. I'm[/color]
    sure[color=blue]
    > that this script can help quiet a lot of people. ;)
    >
    > Ciao
    >
    > Stijn
    >
    >[/color]


    Comment

    • KAH

      #3
      Re: split -command ??

      "nieuws" <stijnthe1st@ya hoo.com> wrote in
      news:Lao0b.7495 8$F92.8325@afro dite.telenet-ops.be:
      [color=blue]
      > I have a txt file with the following data :
      >
      > 1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich 2.37;
      > 4. John Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12;[/color]
      [color=blue]
      > $position=split ("\;", $textfile); //splitting in an array with the
      > semicolon, the result is an array with rows like this 1. stijn piot
      > 58.12
      > $max = count($position ); // counting the size of the array[/color]

      Why are you escaping the semicolon? It does not need escaping, you
      should've gotten an error about it (try setting your ERROR_REPORTING to
      E_ALL while testing).

      [color=blue]
      > $i =0;
      > For ($i=0; $i<$max; $i++) {
      > $time = split ("^[0-9]+\.[[:space:]]", $position[$i]); // the row
      > 1.
      > stijn piot 58.12 is splitted according "number space", the result is
      > stijn piot 58.12
      > $name = split ("[0-9]+\.[0-9]+", $time[1]);
      > echo $name[0]; // the previous result stijn piot 58.12 is splitted
      > according "number.number" , the final result should be stijn piot
      > }[/color]

      I don't understand what you're doing here. Why split it several times,
      with those strange patterns? Just split it once (looks to me like you're
      only interested in the name, and not the time):

      $name = split ("[0-9]", $position[$i]);

      I recommend you use the PCRE functions, they're faster and have a very
      good (IMHO) RegEx engine.



      KAH

      Comment

      Working...