Kingy Posted January 24, 2008 Share Posted January 24, 2008 I am trying to get the score off this webpage http://content-nz.cricinfo.com/ausvind/engine/current/match/291354.html?view=scorecard i've never really used file_get_contents() before but i've got <?php $content=file_get_contents("http://content-nz.cricinfo.com/ausvind/engine/current/match/291354.html?view=scorecard"); echo "$content"; ?> which works so i've started well lol. All i'm looking to get is the one line: "Total (2 wickets; 24.6 overs) 87 (3.48 runs per over)" And i realise that look at that in html puts it over multiple lines so how would i go about retrieving that? Quote Link to comment https://forums.phpfreaks.com/topic/87480-get-contents/ Share on other sites More sharing options...
cooldude832 Posted January 24, 2008 Share Posted January 24, 2008 this will get you started ask me if you don't get the concept <?php $file = "http://content-nz.cricinfo.com/ausvind/engine/current/match/291354.html?view=scorecard"; $data = file_get_contents($file); $data = strip_tags($data, "<table>"); $data = explode("<table",$data); $table_9 = explode("\n",$data[9]); $temp = explode(" ",$table_9[3]); $info['test_no'] = $temp[count($temp)-1]; $info['teams'] = $table_9[8]; $info['season'] = $table_9[12]; unset($temp); $table_10 = explode("\n",$data[10]); $temp = explode(" on ",$table_10[4]); $info['location'] = $temp[0]; $info['date'] = $temp[1]; unset($temp); $table_11 = explode("\n",$data[11]); $i = 24; $players = 0; $stop = 0; while($stop != 1){ $players++; $info['player'][$players]['name'] = $table_11[$i]; $i++; $info['player'][$players]['action'] = $table_11[$i]; $i++; $info['player'][$players]['R'] = $table_11[$i]; $i++; $info['player'][$players]['B'] = $table_11[$i]; $i++; $info['player'][$players]['4s'] = $table_11[$i]; $i++; $info['player'][$players]['6s'] = $table_11[$i]; $i++; $info['player'][$players]['SR'] = $table_11[$i]; $i = $i+5; if(stristr($table_11[$i],"Extras")){ $stop = 1; } if($i > 100){ $stop = 1; } } $i++; $info['extras'] = $table_11[$i]; $i++; $info['R_extra'] = $table_11[$i]; $i = $i+19; $info['W_total'] = $table_11[$i]; $i++; $info['R_total'] = $table_11[$i]; $i++; $info['R_over'] = $table_11[$i]; print_r($info); print_R($data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447454 Share on other sites More sharing options...
Kingy Posted January 24, 2008 Author Share Posted January 24, 2008 Looked a little confusing to start off with, but now that i am looking through it and once i got rid of the print_r($data), i'm understanding it quite easily, Quote Link to comment https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447474 Share on other sites More sharing options...
cooldude832 Posted January 24, 2008 Share Posted January 24, 2008 the print_r is to read what is happening, i found the structure to be a table built so I got rid of all other tags but tables then I look at each table manually and read the data on it line by line ($table_9 = explode("\n",$data[9]); then I read the lines and did the work on those Quote Link to comment https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447477 Share on other sites More sharing options...
Kingy Posted January 24, 2008 Author Share Posted January 24, 2008 can i ask what $i is and what its counting? Quote Link to comment https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447494 Share on other sites More sharing options...
cooldude832 Posted January 24, 2008 Share Posted January 24, 2008 I felt that the player's stats was an area that could be "dynamic" so when you go to use this on another page I wanted to make it logically loop through that section reading stats until it senses the end of the stats area (that stristr($table_11[$],"Extra") that makes sure we didn't reach the end of hte player list Quote Link to comment https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447496 Share on other sites More sharing options...
Kingy Posted January 24, 2008 Author Share Posted January 24, 2008 This is really good work cooldude, its actually really helpful for me, i can go through and choose what table to explode, and then which data i want to use. Thanks heaps Another question though, if i wanted to select only the batsmen who are "not out" what would i need to do Quote Link to comment https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447516 Share on other sites More sharing options...
cooldude832 Posted January 24, 2008 Share Posted January 24, 2008 in that while loop set a second counter index J that value is equal to that given position in the exploded table and say <?php While(){ $J = $i + $batter_pos; if($table_11[$j] == "Batter Out"){ //Get data } else{ //Just add the correct correlation to $i } ?> $batter_pos is what ever that data point is 24+ some number Quote Link to comment https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447518 Share on other sites More sharing options...
Kingy Posted January 24, 2008 Author Share Posted January 24, 2008 don't really understand what your saying there, could u explain a little more please Quote Link to comment https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447521 Share on other sites More sharing options...
cooldude832 Posted January 24, 2008 Share Posted January 24, 2008 $i sets the starting position of the player starting data (I belive it was 24 in that table) it loops through this until it finds a match for the line that has the word "Extra" in it and breaks the loop The thing that says if a player is out is line 26 I believe so in your case $j would be 2 (look at the print_r for table_11 to see this clearer Then you just test if $table_11[$j] says the player was "out" (based on the text there) if they were then count the data else ignore it. Better way to handle this is collect all in a db and do selective querying later on. Quote Link to comment https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447528 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.