Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: PHP Parser

  1. #1
    Regular Coder UrbanTwitch's Avatar
    Join Date
    Apr 2006
    Posts
    485
    Thanks
    44
    Thanked 1 Time in 1 Post

    PHP Parser

    I am trying to get some information using a parser I am making.

    Here is my code
    PHP 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 :
    Code:
    Notice: Undefined offset: 1 in C:\wamp\www\wekaboard\zbcode.php on line 9
    Hi Dorith
    Here is what I am trying to get:
    Code:
    <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?
    Formerly MysticScript

    6+ years club

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    try this

    PHP Code:
    <?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 Code:
    <?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

    Code:
    <!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>
    Last edited by Fou-Lu; 08-22-2009 at 10:16 PM. Reason: No reason for this to be four posts.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •