SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with working code

    PHP Code:
    <?php
    $html 
    file_get_contents("http://neopets.com/gameroom.phtml");

    preg_match("#<td width=100><a href='/games\/(.*)'><img src='http:\/\/images\\.neopets.com\/new_games\/n(.*)' width=100 height=100 border=0></a></td>#i"$html$match);

    $find = array("<td width=100>","</td>");
    $replace = array("","");
    $do str_replace($find,$replace,$match[0]);
    echo 
    $do;
    ?>
    I have been using this code to fetch information from a site called neopets.com , the code has been working fine until recently. Now I have been getting this message.

    Code:
    Notice: Undefined offset: 0 in /home/xptutor/public_html/scripts/game.php on line 8
    Checking through I see nothing wrong and it was working before hand. Perhaps you fine people can help me figure out what is wrong. Thanks

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $match[0] is empty, it doesnt exist.

    that means your regex pattern didnt match.

    echo $html and see why.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I replaced the link with a functioning one , http://neopets.com/games/arcade.phtml , and then I removed the [0], which causes it now to just say "array"

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    look at the html source.

    where could your pattern possibly match? it cannot.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn Neopets, changing it on me, I'll just need to go through and pick out what I need

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Really damn Neopets.

    I can't find the code regarding the Featured Game, Could I please get some help?

    The websites url is http://www.neopets.com/games/arcade.phtml

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone help? I'll offer a small incentive

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll pay for this help, $6 bucks right there, for some simple code

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anybody, I really need help with this.

  10. #10
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure which bits you want to catch exactly but this will grab the id of the featured game (into $match[1]):

    preg_match("#featured_game_label.*?game_id=([0-9]+)#ism", $html, $match);

    You'll have to specify exactly which bits you want and what you want to do with it if you need more than just the featured game id.

  11. #11
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I want exactly is to be able to see what the games picture is, the category would not have to necessarily shown, but it is a plus, so ideally, I just need the picture rendered

  12. #12
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anybody, again, it seems simple enough and you will get paid.

  13. #13
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anybody please?

  14. #14
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code should work, but it do the match 2 times, so I put it in a function.

    PHP Code:
    <?php

    // Get the HTML.
    $html file_get_contents('http://www.neopets.com/games/arcade.phtml');

    // Strip all HTML tags and change all type of whitespaces to space.
    function html_inline_plaintext($x) {
        return 
    trim(preg_replace('~\s+~'' 'strip_tags($x)));
    }

    // Gets the featured game.
    function get_featured_game($html) {
        
    // Match various things...
        
    preg_match('~<div id=\'featured_game_link\'><a href=(.*?game_id=([^>]+))>(.*?)</a></div><div[^>]*>(.*?)</div>~si'$html$link_match);
        
    preg_match('~<div id=\'featured_game_icon\'>.*?<img border=0 src=\'(.*?)\'>~si'$html$icon_match);
        
    // And return an array.
        
    return array(
            
    'name'  => html_inline_plaintext($link_match[3]),
            
    'desc'  => html_inline_plaintext($link_match[4]),
            
    'id'    => html_inline_plaintext($link_match[2]),
            
    'link'  => html_inline_plaintext($link_match[1]),
            
    'image' => html_inline_plaintext($icon_match[1]),
        );
    }

    // Fetch the featured game.
    $featured_game = (get_featured_game($html));

    // Display the result.
    echo '
    <h1>Featured Game</h1>
    <img src="' 
    $featured_game['image'] . '" alt="Icon" style="float: left; margin-right: 1em;" />
    <h2>' 
    $featured_game['name'] . '</h2>
    <p>' 
    $featured_game['desc'] . '</p>
    <p>Game ID: ' 
    $featured_game['id'] . '</p>
    <p><a href="' 
    $featured_game['link'] . '">Link to game</a></p>';

    ?>


Bookmarks

Posting Permissions

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