Jump to content

get info from a html page with php


blacknight

Recommended Posts

im looking for a way to get info from a html page in php i have a code that was sapose to work but no longer does any one have any ideas on how to make it work? here is the code

 

<?php

$realmstatus = utf8_decode('Total Characters:');


$realmstatus = trim($realmstatus);

$url = 'http://www.warcraftrealms.com/census.php?serverid=802&factionid=-1&minlevel=1&maxlevel=70&servertypeid=1';

$current_time = date('i')*1;


	if (function_exists('curl_init'))
	{
		$fp = curl_init( $url );
		curl_setopt($fp, CURLOPT_HEADER, 0);
		curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
		$html = curl_exec($fp);
		if ( curl_errno($fp) )
		{
			$ch_err = 1;
		}
		else
		{
			$ch_err = 0;
		}
		curl_close($fp);
	}
	else
	{
		$html = @file_get_contents($url);
	}


//==========[ HTML PARSER ]============================================================

if (isset($html) && $html)
{
	if (!preg_match('/\<tr(.(?!\<tr))*('.str_replace("'",'&#039;',$realmstatus).')(.(?!\<\/tr))*/si',$html,$matches))
	{
		$err = 1;
	}
	elseif (preg_match_all('/^.*color: #([0-9a-fA-F]+);\">([^<]*)/m',$matches[0],$row) != 3)
	{
		$err = 1;
	}

	//print_r($row);
	//die();
}

else
{
	$err = 1;
}


?>

Link to comment
Share on other sites

it wont return the data like it should really i havent used this type of code much the page has numerical values on it and i want to get them on to my page to put them in a database. The phrase "Total Characters:" is in a table and looks like

        <tr>
            <td class="category">Total Characters:</td>
            <td><b>9,352</b></td>
        </tr>

this i am trying to return the number 9,352 and its not worken..

Link to comment
Share on other sites

Try using the snoopy library instead of cURL to get the page itself, here is the snoopy library:

 

http://claninfectionist.com/misc/snoopy.phps

 

Just save that code as a php file, then include it in your page.  The code to get a remote page and store the source code of that page would be this:

 

include('snoopy.php');
$snoopy = new Snoopy;
if($snoopy->fetch("URL"))
$text = ($snoopy->results);

 

The variable $text would contain the source code of the page you requested.  This is just a much simpler method of retrieving pages.  That being said, to get the number that you need, try this(after calling up the page with snoopy):

 

preg_match("|<td><b>(.+?)</b></td>|", $text, $match);

 

This would be a VERY tentative way of doing it, since there could be multiple lines that follow that format, but if there are not, then this would work, it will return two values to the array $match, your array will be this:

 

Array
(
    [0] => <td><b>9,352</b></td>
    [1] => 9,352
)

 

but when you do print_r($match), it will return this:

 

Array ( [0] => 9,352 [1] => 9,352 )

 

Anywho, I hope this helps at least a little, and like I said, feel free to use the snoopy library, its way easier than cURL.

Link to comment
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.