Would someone be kind enough to tell me the sort of PHP code required for my tennis website to use the official rankings on ATP.com so that it updates the ranking of a particular player on my own website automatically? EDIT: http://www.atptennis.com/3/en/rankin...em/default.asp
I looked at the site, and couldn't find what you're talking about. Can you post a link to the site where you want to extract the content from?
Thanks for your response... http://www.atptennis.com/3/en/rankings/entrysystem/default.asp I want to extract the ranking next to 'Murray, Andy'... he's 8th there at the moment.
<?php $data = file_get_contents('http://www.atptennis.com/3/en/rankings/entrysystem/default.asp'); preg_match_all('/<div class="entrylisttext">(.*?)<\/div>/s', $data, $matches); $int = 0; // Free some memory unset($matches[0]); foreach ($matches[1] AS $match) { preg_match('/^([^\(]+)\(([^\)]+)\)/', @strip_tags($matches[1][++$int]), $playerinfo); $players[trim($playerinfo[1])] = array( 'points' => @strip_tags($matches[1][++$int]), 'pos' => @strip_tags($matches[1][++$int]), 'tourns' => @strip_tags($matches[1][++$int]), 'country' => $playerinfo[2] ); $int++; } // Usage example for $players array: foreach ($players AS $player => $playerinfo) { echo "<strong>{$player}:</strong><br />\n"; echo "Points: {$playerinfo['points']}<br />\n"; echo "Position: {$playerinfo['pos']}<br />\n"; echo "Tournaments: {$playerinfo['tourns']}<br />\n"; echo "Country: {$playerinfo['country']}<br /><br />\n"; } // Or get the info for a specific player. (The name has to be exact) $info = $players['Murray, Andy']; echo '<pre>' . print_r($info, true) . '</pre>'; ?> PHP:
Wow thank you, that should come in real handy! Can you tell me how to do the same for his profile page, I realise now that his profile page is better to use just for ranking. I tried using his profile page instead, using this code: <?php $data = file_get_contents('http://www.atptennis.com/3/en/players/playerprofiles/?playernumber=MC10'); $regex = '/<div class="data">Current ATP Ranking - Singles:</div></td><td valign="middle"><span class="lines"> (.+?) </span>/'; preg_match($regex,$data,$match); var_dump($match); echo $match[1]; ?> Code (markup): But that simply displayed 'array(0) { }'
Lol, you could have thought of that earlier. Anyway... Change the regex to: $regex = '/<div class="data">Current ATP Ranking - Singles:<\/div>\s*<\/td>\s*<td valign="middle"><span class="lines">\s*(\d+)\s*<\/span>/'; PHP:
I'm so sorry but I will keep your code and it will probably come in useful in the near future. Thanks for the revised regex code although it displays too much information. I only want the number '8' displayed. This is what is displayed: array(2) { [0]=> string(125) " Current ATP Ranking - Singles: 8 " [1]=> string(1) "8" } 8
This code displays nothing, <?php $data = file_get_contents('http://www.atptennis.com/3/en/players/playerprofiles/?playernumber=MC10'); $regex = '/<div class="data">Current ATP Ranking - Singles:<\/div>\s*<\/td>\s*<td valign="middle"><span class="lines">\s*(\d+)\s*<\/span>/'; echo $match[1]; ?> Code (markup):
Because you took the preg_match() function out. JUST take out the var_dump() of the piece of code where it displayed too much.
It would work the same probably, but it's a little bit more specific the way I did, because \d only matches numbers, while .+? matches any character except new lines. (Unless the s modifier has been specified)
How can I make it so that 'new lines' are not parsed? Also, is there a way to make it look in the source code bottom to top?