[PHP] Show Pet's Battle Stats

Discussion in 'Code Snippets and Tutorials' started by Belaarx, Nov 30, 2008.

  1. Belaarx

    Belaarx Level I

    Joined:
    Sep 23, 2008
    Messages:
    135
    Likes Received:
    16
    Location:
    San Diego
    Here is a code snippet tutorial that will display all of a pet's battledome statistics:
    (Note: I didn't want to bother with regex so I just used GSB)
    (Note 2: This script requires that you have the curllib installed in your PHP configuration. (Most hosts do.))

    Code (Text):
    1. <?php
    2.  
    3. function GetStringBetween($string, $start, $end){
    4.         $string = " ".$string;
    5.         $ini = strpos($string,$start);
    6.         if ($ini == 0) return "";
    7.         $ini += strlen($start);  
    8.         $len = strpos($string,$end,$ini) - $ini;
    9.         return substr($string,$ini,$len);
    10. }
    11.  
    12.  
    13.     $pet = $_GET["pet"];
    14.    
    15.     $curl = curl_init();
    16.     curl_setopt($curl, CURLOPT_URL, "http://www.neopets.com/petlookup.phtml?pet=".$pet);
    17.     curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4");
    18.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    19.     $data = curl_exec($curl);
    20.     echo "Strength: " . GetStringBetween($data, "<b>Strength:</b> ", "<br>");
    21.     echo "<br>Defence: " . GetStringBetween($data, "<b>Defence:</b> ", "<br>");
    22.     echo "<br>Movement: " . GetStringBetween($data, "<b>Movement:</b> ", "<br>");
    23.     echo "<br>Intelligence: " . GetStringBetween($data, "<b>Intelligence:</b> ", "<br>") . "<br><br>";
    24.     echo "BD Stats (Win/Lose):<br>";
    25.     echo "<font color=green><b>" . GetStringBetween($data, "<b>Won:</b>", "<br />") . "</b></font> /<font color=red><b>" . GetStringBetween($data, "<b>Lost:</b>", "<br />") . "</b></font>";
    26.    
    27. ?>
    When the script is accessed like this (script.php?pet=featheralley), here is the output: