Jump to content

Recommended Posts

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.  ;D

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.

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

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.

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.

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.

 

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_#]+

 

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.

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.

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>

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>

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

 

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>

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.