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
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.
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
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)
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.
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)?