WEvans Posted May 31, 2007 Share Posted May 31, 2007 Hello, First I'd like to say I'm a newbie when it comes to php. So please respond in "php 4 dummies" language. Thank you. I'm trying to come up with a way to have a php file get information from a webpage. The web page shows top 100 players and stats for a league. It shows it in a table, top row has headings, I won't need these entries, the next row starts the standings. I just need to pull the names from the second column, in ranking order. i.e. 1st,2nd,3rd..... There is one constant that I can possibly use when searching the html file. ');">NAMEISHERE< So I pretty much want a php file to open www.somesite.com/standingspage.php?rank=0-100, search the source code for the above code, select "NAMEISHERE" which btw is of course never constant and could contain #'s and "_", hold each name in an array (maybe an easier way to do it), then "print" them in their ranked order. i.e. 1. NAMEISHERE 2. NAMEISHERE 3. ect. I'm guessing this might need a while loop as well? Any help would be great. Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/ Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 (1) Find some consistency in the HTML surrounding your target. (2) Write a regular expression matching that consistency. (3) Use preg_match_all() to find all of your targets. (4) Take the match array generated by preg_match_all() and have your way with it. You probably want to use a foreach() loop; they were made for arrays. Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265323 Share on other sites More sharing options...
mpharo Posted May 31, 2007 Share Posted May 31, 2007 So your information is stored in a database....it is much easier than what the previous post said... You just need to do something like this... //ASC will sort 1 to 100 DESC will sort 100 to 1 $select = mysql_query("SELECT * FROM table ORDER BY rank LIMIT 0, 100 ASC") or die(mysql_error()); while ($sql=mysql_fetch_array($select)) { echo "Rank: " . $sql[rank]; echo "User: " . $sql[user]; } You will obviously need to adapt it to your table structure, but you should get the idea. Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265328 Share on other sites More sharing options...
AndyB Posted May 31, 2007 Share Posted May 31, 2007 So your information is stored in a database....it is much easier than what the previous post said... Judging by the original poster's question, I'd expect the raw data exists in a database - someone else's database. Wildbug's solution is what you'll need in that case. Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265330 Share on other sites More sharing options...
TwiztedCupid Posted May 31, 2007 Share Posted May 31, 2007 oops Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265331 Share on other sites More sharing options...
mpharo Posted May 31, 2007 Share Posted May 31, 2007 I stand corrected then, I didnt catch that part in the original post. Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265333 Share on other sites More sharing options...
WEvans Posted May 31, 2007 Author Share Posted May 31, 2007 Thank you Wildbug. But I have no idea how to code it. I do understand what you're saying, just can't put it to code. lol My consistancy is as follows ');">NAMEISHERE</a> Is this how my preg_match_all() code should be? preg_match_all("(\'\);">)\"[a-z][A-Z][0-9][_]\")(.*?)</a>)s", $results); Then create a foreach(results) to display the results. Would I just use fread to get the information in the first place or what? Thank you for your patience and help. Like I said before I'm still a newb. Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265334 Share on other sites More sharing options...
saf Posted May 31, 2007 Share Posted May 31, 2007 What is the website url that you are trying to read? (If I can see what exactly the html code generated looks like, I might be of some help) Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265342 Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 Ditto, saf. Is that really all the consistency you have for your target? It might be okay, but it looks a little common. Seeing the HTML page might help. preg_match_all('/\);">(.+?)<\/a>/i',$html,$matches); for ($i = 1; $i < count($matches[1]); $i++) echo $i . '. ' . $matches[1][$i-1], "<br />\n"; // If you're sure about the legal name characters, you can replace // (.+?) with [A-Z0-9_#]+ Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265528 Share on other sites More sharing options...
WEvans Posted May 31, 2007 Author Share Posted May 31, 2007 http://www.myleague.com/php/ladderStats/standings.php?ladder=crazyhouse&slave=cgi8&action=byRank&rStart=1&rEnd=100 That is the url I'm trying to read. Is that really all the consistency you have for your target? It might be okay, but it looks a little common. Yeah as common as it seems, it really is the only constant that surrounds the text I want to extract from the page. If you check out the source code, I could include more of the constant, but I was just trying to keep it simple, less to search for in the source code. Thank you all again for your time. Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265672 Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 preg is great and all, and does wonders. But I never found it to suit my needs for parsing files. <?php $file = file_get_contents('http://www.myleague.com/php/ladderStats/standings.php?ladder=crazyhouse&slave=cgi8&action=byRank&rStart=1&rEnd=100'); $data = split("onClick=\"alert('", $file); array_shift($data); // remove first element it is useless. foreach ($data as $name) { list($oneName) = split($name, "has been a member of this league for"); $names[] = $oneName; } echo '<pre>',print_r($names),'</pre>'; ?> See how that works for ya. Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265685 Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 Tested and working: <html><body><pre><?php $html = file_get_contents('http://www.myleague.com/php/ladderStats/standings.php?ladder=crazyhouse&slave=cgi8&action=byRank&rStart=1&rEnd=100'); preg_match_all('|<td align="center" >(\d+)</td><td.*?><a id="linklabel".*?>(.*?)</a>|is',$html,$matches,PREG_SET_ORDER); foreach ($matches as $match) echo "$match[1]: $match[2]\n"; ?> </pre></body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265713 Share on other sites More sharing options...
saf Posted May 31, 2007 Share Posted May 31, 2007 Agreed...this should do the job Tested and working: <html><body><pre><?php $html = file_get_contents('http://www.myleague.com/php/ladderStats/standings.php?ladder=crazyhouse&slave=cgi8&action=byRank&rStart=1&rEnd=100'); preg_match_all('|<td align="center" >(\d+)</td><td.*?><a id="linklabel".*?>(.*?)</a>|is',$html,$matches,PREG_SET_ORDER); foreach ($matches as $match) echo "$match[1]: $match[2]\n"; ?> </pre></body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265840 Share on other sites More sharing options...
WEvans Posted May 31, 2007 Author Share Posted May 31, 2007 Wildbug thank you for your responce. That's exactly what I wanted. Now I'm trying to divide the rusults up in 2 columns. i.e. 1. name 11. name 2. name 12. name 3. name 13. name ect. I made an adjustment to the code you sent and come up with this so far. <?php $html = file_get_contents('http://www.myleague.com/php/ladderStats/standings.php?ladder=crazyhouse&slave=cgi8&action=byRank&rStart=1&rEnd=20'); preg_match_all('|<td align="center" >(\d+)</td><td.*?><a id="linklabel".*?>(.*?)</a>|is',$html,$matches,PREG_SET_ORDER); $i = 0; echo "<table width=\"50%\">"; foreach ($matches as $match){ $rank = "$match[1]. $match[2]"; $i = $i +1; if ($i <=10) echo "<tr><td>$rank</td></tr>"; } echo "</table>"; ?> That will display the first 10 in a table for me. I want to display the next 10 into a column right next to the first one, within the same table. I've tried a few things, and it's not even close to what I want. lol Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265867 Share on other sites More sharing options...
saf Posted May 31, 2007 Share Posted May 31, 2007 This should do it...Let me know if it errs. <html><body><pre> <table cellspacing=0 cellpadding=3 border=0> <?php $html = file_get_contents('http://www.myleague.com/php/ladderStats/standings.php?ladder=crazyhouse&slave=cgi8&action=byRank&rStart=1&rEnd=100'); preg_match_all('|<td align="center" >(\d+)</td><td.*?><a id="linklabel".*?>(.*?)</a>|is',$html,$matches,PREG_SET_ORDER); $NumPerCol=10; //The number of entries you want per column $MaxPrint=20; //The total number of entries you want printed for($i=0; $i<$NumPerCol; $i++){ $TotalCols=Ceil($MaxPrint/$NumPerCol); $Col=1; echo '<tr>'; while($Col <= $TotalCols) { $offset=$i+($Col*$NumPerCol)-$NumPerCol; echo '<td>'; if($offset+1<=$MaxPrint) echo $matches[$offset][1].': '.$matches[$offset][2]; else echo ' '; echo '</td>'; $Col++; } echo '</tr>'; } ?> </table> </pre></body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265891 Share on other sites More sharing options...
WEvans Posted May 31, 2007 Author Share Posted May 31, 2007 Everything works perfect. Thanks for your help everyone. Quote Link to comment https://forums.phpfreaks.com/topic/53672-solved-use-php-to-read-and-display-text-from-html-page/#findComment-265915 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.