Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/87480-get-contents/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447454
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447477
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447496
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447516
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447518
Share on other sites

$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.

Link to comment
https://forums.phpfreaks.com/topic/87480-get-contents/#findComment-447528
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.