Jump to content

Grabbing text from a site and adding to variables?


sean123

Recommended Posts

I'm creating a highscore grabber for an online game but I'm having a problem. I want it to grab the users stats and put each stat in a variable so kills would be in 1 and deaths would be in another. I've done the grabbing but not the putting each stat in a variable. This is what I get after I grab the stats:

 

username  Level: 25 Ratio: 1.54 Rank: 3266 th Kills: 73761 Deaths: 9977 Experience: 674534632

 

I want it so it's like this:

 

$name = "username"; 

$level = "25";

$ratio = "1.54";

$rank = "3266 th";

$kill = "73761";

$death = "9977";

$exp = "674534632";

 

And removing anything before the colon. Is this possible?

Link to comment
Share on other sites

You need to add some sort of comma or ";"...

 

I took off the Username Level and stuff and put the data in seperate columns

 

25;1.54;3266th;73761;9977;674534632';

 

 

Try

 

<?php
$stats = 25;1.54;3266th;73761;9977;674534632';
            //username   Level: 25 Ratio: 1.54 Rank: 3266 th Kills: 73761 Deaths: 9977 Experience: 674534632 

$break = explode(';',$stats);
$name = "username";
$level = str_replace($break[0]);
$ratio = str_replace($break[1]);
$rank = str_replace($break[2]);
$kill = str_replace($break[3]);
$death = str_replace($break[4]);
$exp = str_replace($break[5]);
?>

Link to comment
Share on other sites

Will it ALWAYS be formatted like this?  Have a look at the substring functions.  www.php.net/substr

 

Here's one portion:

$deaths = substr($var, stripos($var, "Deaths: "),stripos($var, "Experience:"));//<--should return Deaths: 9977
$deaths = substr($deaths, stripos($var, " "));  //  <--should return 9977

 

Here's a quick explanation of what's happening...

substr($var, 3, 6); //  <--will return the 3rd thru the 6th character (zero based--the first char is actually char zero)
stripos($var, "Deaths:  ");//<--will return the position within the variable where "Deaths:  " begins.

Link to comment
Share on other sites

You need to add some sort of comma or ";"...

 

I took off the Username Level and stuff and put the data in seperate columns

 

25;1.54;3266th;73761;9977;674534632';

 

 

Try

 

<?php
$stats = 25;1.54;3266th;73761;9977;674534632';
            //username   Level: 25 Ratio: 1.54 Rank: 3266 th Kills: 73761 Deaths: 9977 Experience: 674534632 

$break = explode(';',$stats);
$name = "username";
$level = str_replace($break[0]);
$ratio = str_replace($break[1]);
$rank = str_replace($break[2]);
$kill = str_replace($break[3]);
$death = str_replace($break[4]);
$exp = str_replace($break[5]);
?>

 

little error

 

<?php
$stats = '25;1.54;3266th;73761;9977;674534632';
            //username   Level: 25 Ratio: 1.54 Rank: 3266 th Kills: 73761 Deaths: 9977 Experience: 674534632 

$break = explode(';',$stats);
$name = "username";
$level = str_replace($break[0]);
$ratio = str_replace($break[1]);
$rank = str_replace($break[2]);
$kill = str_replace($break[3]);
$death = str_replace($break[4]);
$exp = str_replace($break[5]);
?>

 

Donie, Its easier for him to format it like that anyways, he doesnt need to names like LEVEL, he can just add that himself later.

Link to comment
Share on other sites

Donie, Its easier for him to format it like that anyways, he doesnt need to names like LEVEL, he can just add that himself later.

 

Agreed--that would be easier.  But since he's screen scraping, I'm guessing that he doesn't have access to the data to reformat it.  If he CAN reformat it, I'd go with your suggestion.  If he CAN'T, my suggestion would work well.

Link to comment
Share on other sites

Donie, Its easier for him to format it like that anyways, he doesnt need to names like LEVEL, he can just add that himself later.

 

Agreed--that would be easier.  But since he's screen scraping, I'm guessing that he doesn't have access to the data to reformat it.  If he CAN reformat it, I'd go with your suggestion.  If he CAN'T, my suggestion would work well.

 

As I said you can just add it later, like right when you set the variables you can always "reformat"

 

$name = "username";
$level = "User Level:" . str_replace($break[0]);

Link to comment
Share on other sites

sean123,

This is a pain in the ass to write :D But I'll try.

 

<?php
function charAt($str, $pos) {
    return (substr($str, $pos, 1)) ? substr($str, $pos, 1) : -1;
}
function remBdrySp ($str) {
    $f = 0; $l = strlen($str)-1;
    while ($f < $l) {
        if (charAt(" ", $f) == -1 && charAt(" ", $l) == -1) break;
        if (charAt(" ", $f) != -1) $f ++;
        if (charAt(" ", $l) != -1) $l++;
    }
    return substr($str, $f, $l+1);
}
$daStr = "
username   Level: 25 Ratio: 1.54 Rank: 3266 th Kills: 73761 Deaths: 9977 Experience: 674534632";
$name = substr($daStr, 0, strpos($daStr, " "));
$daStr = explode(": ", $daStr); $e = array();
for ($k=1; $k < count($daStr); k++) {
    $e[$k - 1] = substr($daStr[$k], 0, strrpos($daStr[$k], " "));
}
$level = $e[0];
$ratio = $e[1];
$rank = $e[2];
$kill = $e[3];
$death = $e[4];
$exp = $e[5];
?>

 

Ken

Link to comment
Share on other sites

Just tried it didn't work:

 

$txt = ereg_replace( '  ', '', $txt);

 

Mine Works, just that you need to insert the user data into the format i showed you.

 

<?php
$stats = '25;1.54;3266th;73761;9977;674534632';
            //username   Level: 25 Ratio: 1.54 Rank: 3266 th Kills: 73761 Deaths: 9977 Experience: 674534632 

$break = explode(';',$stats);
$name = "username";
$level = str_replace($break[0]);
$ratio = str_replace($break[1]);
$rank = str_replace($break[2]);
$kill = str_replace($break[3]);
$death = str_replace($break[4]);
$exp = str_replace($break[5]);
?>

Link to comment
Share on other sites

Can't use that code :( And is it possible to get what line the stat is on then store it in a variable? I've got it so it's like this:

 

  webmonster 

Level:

25

Ratio:

1.54

Rank:

4266 th

Kills:

73761

Deaths:

9977

Experience:

674534632

 

So if I wanted to put the number of kills in a variable I would put the line number somewhere and it'll store the number 73761.

Link to comment
Share on other sites

use preg match

 

<?php
  header('Content-type: text/plain');
  $str="username   Level: 25 Ratio: 1.54 Rank: 3266 th Kills: 73761 Deaths: 9977 Experience: 674534632";

  preg_match('@([^\s]+)\s*Level: (\d+)\s+Ratio:\s+([\d.]+)+\s+Rank:\s+(\d+).+?Kills:\s+(\d+)\s+Deaths:\s+(\d+)\s+Experience:\s+(\d+)$@',$str,$matches);
  print_r($matches);
?>
/* Output:
Array
(
[0] => username   Level: 25 Ratio: 1.54 Rank: 3266 th Kills: 73761 Deaths: 9977 Experience: 674534632
[1] => username
[2] => 25
[3] => 1.54
[4] => 3266
[5] => 73761
[6] => 9977
[7] => 674534632
)
*/

 

as u can see everything is captured :)

well, item 0 can be ignored, as that is just the matched string.

Link to comment
Share on other sites

I tried:

 

preg_match('@\s*\s*([^\s]+)\sLevel: (\d+)\s+Ratio:\s+([\d.]+)+\s+Rank:\s+(\d+).+?Kills:\s+(\d+)\s+Deaths:\s+(\d+)\s+Experience:\s+(\d+)$@',$str,$matches); //print_r($matches);

 

Didn't work :( If I send you a private message with the full source can you try it?

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.