...

View Full Version : PHP Parser



UrbanTwitch
08-22-2009, 09:00 PM
I am trying to get some information using a parser I am making.

Here is my code

<?php
if (!$_GET['id']) {
die("Please specify a member ID using ?id=<u>1245</u>");
}
$file = file_get_contents("http://zbcode.com/profile/".$_GET['id']);
if (preg_match("/<td>No Member was found/",$file) == 1) die("Member not Found");
preg_match("/<th colspan=\"2\" class=\"l\">(.*?)<\/th>/",$file,$name);
preg_match("/<dt>Posts:<\/dt><dd>(.*?)<\/dd>/",$file,$post);
echo ("Hi $name[1] <br />$post[1]");
?>
However when I run it.. I get :


Notice: Undefined offset: 1 in C:\wamp\www\wekaboard\zbcode.php on line 9
Hi Dorith

Here is what I am trying to get:

<dt>Posts:</dt>
<dd>527</dd>

Where 527 is the number I am getting. Of course, It updates.
Example profile: http://zbcode.com/profile/375164

What am I doing wrong?

Phil Jackson
08-22-2009, 10:00 PM
try this


<?php
$id = 375164;
$file = file_get_contents("http://zbcode.com/profile/".$id);
if (preg_match("/<td>No Member was found/",$file) == 1) die("Member not Found");
if(preg_match("/<th colspan=\"2\" class=\"l\">(.*?)<\/th>/",$file,$name))
{
if(preg_match("/<dt>Posts:<\/dt><dd>(.*?)<\/dd>/",$file,$post))
{
echo "Hi ".$name[0]." <br />".$post[0];
}
else
{
echo "Found no posts";
}
}
else
{
echo "couldn't find this thing";
}
?>

or even better


<?php
$id = 375164;
$file = file_get_contents("http://zbcode.com/profile/".$id);
if (preg_match("/<td>No Member was found/",$file) == 1) die("Member not Found");
if(preg_match("/<th colspan=\"2\" class=\"l\">(.*?)<\/th>/",$file,$name))
{
if(preg_match("#<dt>Posts:<\/dt>\s*<dd>(.*)<\/dd>#is",$file,$post))
{
echo "Hi ".$name[0]." <br />".$post[0];
}
else
{
echo "Found no posts";
echo "<xmp>".$file."</xmp>";
}
}
else
{
echo "couldn't find this thing";
echo "<xmp>".$file."</xmp>";
}
?>

white space occurred between </dt>[[[[[]here[]]]]]<dd> which you didn't take into account. Also using <xmp> to debug is very useful mate!

out put being



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

Hi <th colspan="2" class="l">Dorith</th> <br /><dt>Posts:</dt>
<dd>530</dd>
<dt>Group:</dt>
<dd>100 Club</dd>
<dt>Member</dt>
<dd>#1854</dd>
<dt>Joined:</dt>
<dd>Dec 23, 2008</dd>

<dd class="spacer"></dd>
</dl>
<dl class="user_profile"><dt>Coding language</dt><dd>JavaScript</dd><dd class="spacer"></dd>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum