1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Read file and get custom data

Discussion in 'PHP' started by moath11, Jul 6, 2014.

  1. #1
    I'm trying to code a simple php script that open an file and print custom data from it

    Here some of the file contents:

    192.168.2.1 - - [06/Jul/2014:05:00:18 -0700] "GET /dice.php HTTP/1.1" 304 182 "htttp://{localhost}/log.html?varb=74657374" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0"

    it's a log file .

    basically the data i need to get from the file is like this

    the varb in the url 74657374

    and echo that number or store in in a variable.

    for example we have a file "demo.txt" and when i execute the php script the script should open the demo.txt and search for ( varb= ) and get the data after the equal sign (=) then echo it or store in in a variable

    thanks please comment
     
    moath11, Jul 6, 2014 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    You can use the following:

    
    // Your line
    $var = ' 192.168.2.1 - - [06/Jul/2014:05:00:18 -0700] "GET /dice.php HTTP/1.1" 304 182 "htttp://{localhost}/log.html?varb=74657374" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0"';
    // Explode by quotes, since all the data before is static should be good
    $var = explode('"', $var);
    // Parse the get variables
    $var = parse_url($var[3], PHP_URL_QUERY);
    // Parse the string into variables
    parse_str($var, $var);
    // Echo the string, url decoded
    echo urldecode($var['varb']);
    
    Code (markup):
    Another way you can do it is via regular expressions, but I stay away from that unless needed.
     
    ThePHPMaster, Jul 6, 2014 IP
  3. moath11

    moath11 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thank you for replay
    i have try this

    $contents=file_get_contents( 'demo.txt' );
    $pttn='@(varb=\d+)@';
    preg_match_all( $pttn, $contents, $matches );
    print_r( $matches );

    but it not worked correctly can you help me
     
    moath11, Jul 6, 2014 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    var_dump $contents. Code should work fine if it has the contents you posted above.
     
    ThePHPMaster, Jul 6, 2014 IP
  5. moath11

    moath11 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    i will need complete code
    please comment if you can help me wit that
     
    moath11, Jul 6, 2014 IP
  6. moath11

    moath11 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    any help plz
     
    moath11, Jul 9, 2014 IP
  7. Zbee

    Zbee Active Member

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #7
    I personally like exploding stuff, so:

    $contents=file_get_contents('demo.txt');
    $varb = explode("varb=", $content)[1];
    $varb = explode('"', $varb)[0];

    That will give you the varb from the URL.

    It's similar to ThePHPMaster's answer, but his method might break if you change the way logging works, or if you incorrectly sanitize and log some data (leave a " in there that doesn't belong)
     
    Zbee, Jul 11, 2014 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    Note that that last code only works on... PHP 5.3.x and up? Maybe 5.4.x and up. The function[0] setup was added in one of the latter PHP-versions.
     
    PoPSiCLe, Jul 11, 2014 IP
  9. Zbee

    Zbee Active Member

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #9
    What do you mean by
    ?

    Would
    $foo = bar($foo, $bar);
    $bar = $foo[0];​
    work with older versions where
    $bar = bar($foo, $bar)[0];​
    wouldn't (older versions)?
     
    Zbee, Jul 11, 2014 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    Yes, that is correct.
     
    PoPSiCLe, Jul 12, 2014 IP