SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast CorkyMcDoogle's Avatar
    Join Date
    Aug 2006
    Location
    Chesapeake, Maryland
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Returning certain text from a line...

    I'm trying to figure out how to return certain text from an uploaded .txt file.

    Say for instance this is the first line the the file:

    Game #23646719060: Tournament #132259787 - Level I (10/20) - 2009/01/06 20:33:12 ET

    Say I'm trying to get the tournament number. I want to make a function that scans the line and only returns the number "132259787" so I can store it in a database.

    What PHP functions do I need to search the line and pull out what I want?

    Just a little help is fine... I just need a jumpstart. I've been away from this stuff for awhile =P

    Thanks

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $parts split(" "$line); //split line at spaces into an array
    $tournament_number substr($parts[3], 1); 

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $filename 'myfile.txt';
    $lines file($filename);
    foreach(
    $lines as $line){
        
    $parts split(" "$line); //split line at spaces into an array
        
    $tournament_number substr($parts[3], 1); 

    But you must be sure about the line format.. like there must be spaces and words in the same format as you have posted.
    Code:
    Game #23646719060: Tournament #132259787 - Level I (10/20) - 2009/01/06 20:33:12 ET
    The above PHP script won't give you the proper value if the line is something like this:
    Code:
    Tournament #132259787 Game #23646719060: - Level I (10/20) - 2009/01/06 20:33:12 ET
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sscanf() would be an alternative function to use
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  5. #5
    SitePoint Enthusiast CorkyMcDoogle's Avatar
    Join Date
    Aug 2006
    Location
    Chesapeake, Maryland
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys...

    Yes, that will be a problem because the parts that I'm looking for not be in the exact order in every file.

    What's the best way for extracting something that might be in a different place under varying circumstances?

  6. #6
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    $subject file_get_contents("x.txt");

    $pattern '/(?<=[#])\d*/s';

    preg_match_all($pattern$subject$matches);

    print_r($matches); 
    my mobile portal
    ghiris.ro

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then do something like this until someone gives some more compact solution:
    PHP Code:
    $filename 'myfile.txt';
    $lines file($filename);
    foreach(
    $lines as $line){
        
    $tournament_pos strpos($line'Tournament');
        
    $line substr($line$tournament_pos);
        
    $parts split(" "$line);
        
    $tournament_number substr($parts[1], 1);
        echo 
    $tournament_number

    but for this also, you must have the word 'Tournament' somewhere in the line.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SitePoint Enthusiast CorkyMcDoogle's Avatar
    Join Date
    Aug 2006
    Location
    Chesapeake, Maryland
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting somewhere...

    This is the structure of the .txt files that are being read. They are poker tournament hand histories. Any file could have anywhere between 1 or 100 of these hands in them...

    PokerStars Game #23646719060: Tournament #132259787, $6.00+$0.50 Hold'em No Limit - Level I (10/20) - 2009/01/06 20:33:12 ET
    Table '132259787 1' 9-max Seat #1 is the button
    Seat 1: CorkyMcD (1500 in chips)
    Seat 2: mrsnfl111 (1500 in chips)
    Seat 3: SpaceThunder (1500 in chips)
    Seat 4: xxxjoepxxx (1500 in chips)
    Seat 5: jbeverley (1500 in chips)
    Seat 6: wool85 (1500 in chips)
    Seat 7: aarin1 (1500 in chips)
    Seat 8: cromagig (1500 in chips)
    Seat 9: sa1251 (1500 in chips)
    mrsnfl111: posts small blind 10
    SpaceThunder: posts big blind 20
    *** HOLE CARDS ***
    Dealt to CorkyMcD [6h 3s]
    xxxjoepxxx: raises 120 to 140
    jbeverley: folds
    wool85: folds
    aarin1: calls 140
    cromagig: folds
    sa1251: folds
    CorkyMcD: folds
    mrsnfl111: folds
    SpaceThunder: folds
    *** FLOP *** [7h 7c Js]
    xxxjoepxxx: bets 140
    aarin1: calls 140
    *** TURN *** [7h 7c Js] [8c]
    xxxjoepxxx: bets 140
    aarin1: calls 140
    *** RIVER *** [7h 7c Js 8c] [6s]
    xxxjoepxxx: checks
    aarin1: checks
    *** SHOW DOWN ***
    xxxjoepxxx: shows [Ah Jh] (two pair, Jacks and Sevens)
    aarin1: shows [Jd 8d] (two pair, Jacks and Eights)
    aarin1 collected 870 from pot
    *** SUMMARY ***
    Total pot 870 | Rake 0
    Board [7h 7c Js 8c 6s]
    Seat 1: CorkyMcD (button) folded before Flop (didn't bet)
    Seat 2: mrsnfl111 (small blind) folded before Flop
    Seat 3: SpaceThunder (big blind) folded before Flop
    Seat 4: xxxjoepxxx showed [Ah Jh] and lost with two pair, Jacks and Sevens
    Seat 5: jbeverley folded before Flop (didn't bet)
    Seat 6: wool85 folded before Flop (didn't bet)
    Seat 7: aarin1 showed [Jd 8d] and won (870) with two pair, Jacks and Eights
    Seat 8: cromagig folded before Flop (didn't bet)
    Seat 9: sa1251 folded before Flop (didn't bet)
    I want to pull Game #, Tourn #, Buy-in amount, date, etc.

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try this, it will return the tournament number no matter where it occurs in the source string, but it must always be prefixed by 'Tournament #'.



    PHP Code:
    <?php
    $sString 
    '
    Game #23646719060: Tournament #132259787 - Level I (10/20) - 2009/01/06 20:33:12 ET
    Game #23646719060: Level I (10/20) - Tournament #132259787 - 2009/01/06 20:33:12 ET
    Tournament #132259787: Game #23646719060 - Level I (10/20) - 2009/01/06 20:33:12 ET
    Game #23646719060: Level I (10/20) - 2009/01/06 20:33:12 ET - Tournament #132259787
    '
    ;
    preg_match_all('~(?<=Tournament\\s#)\\d+~'$sString$aMatches);
    print_r($aMatches);
    /*
    Array
    (
        [0] => Array
            (
                [0] => 132259787
                [1] => 132259787
                [2] => 132259787
                [3] => 132259787
            )
    )
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

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