sean123 Posted February 16, 2008 Share Posted February 16, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/ Share on other sites More sharing options...
phpSensei Posted February 16, 2008 Share Posted February 16, 2008 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]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468178 Share on other sites More sharing options...
doni49 Posted February 16, 2008 Share Posted February 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468185 Share on other sites More sharing options...
phpSensei Posted February 16, 2008 Share Posted February 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468188 Share on other sites More sharing options...
doni49 Posted February 16, 2008 Share Posted February 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468189 Share on other sites More sharing options...
phpSensei Posted February 16, 2008 Share Posted February 16, 2008 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]); Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468192 Share on other sites More sharing options...
Ken2k7 Posted February 16, 2008 Share Posted February 16, 2008 sean123, This is a pain in the ass to write 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 Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468193 Share on other sites More sharing options...
sean123 Posted February 16, 2008 Author Share Posted February 16, 2008 Thanks I'm using your code Ken2k7 but I'm having a problem. I added it to my code and when it grabs the users stats theres some spaces at the start before it says the username so your code doesn't work Theres 2 spaces at the beginning. Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468292 Share on other sites More sharing options...
redarrow Posted February 16, 2008 Share Posted February 16, 2008 use trim() function......... Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468295 Share on other sites More sharing options...
sean123 Posted February 16, 2008 Author Share Posted February 16, 2008 Just tried it didn't work: $txt = ereg_replace( ' ', '', $txt); Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468320 Share on other sites More sharing options...
doni49 Posted February 16, 2008 Share Posted February 16, 2008 Just tried it didn't work: $txt = ereg_replace( ' ', '', $txt); That's not the trim function. And run it on the car BEFORE the code above runs. Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468466 Share on other sites More sharing options...
sean123 Posted February 16, 2008 Author Share Posted February 16, 2008 Doesn't work after I do it all it outputs is the name. $daStr = trim($txt); Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468508 Share on other sites More sharing options...
thebadbad Posted February 17, 2008 Share Posted February 17, 2008 Wouldn't it be easier to just scrape the data properly, assigning the vars from the start? If that's what you're doing, that is. Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468538 Share on other sites More sharing options...
phpSensei Posted February 17, 2008 Share Posted February 17, 2008 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]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468544 Share on other sites More sharing options...
sean123 Posted February 17, 2008 Author Share Posted February 17, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468906 Share on other sites More sharing options...
laffin Posted February 17, 2008 Share Posted February 17, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468911 Share on other sites More sharing options...
sean123 Posted February 17, 2008 Author Share Posted February 17, 2008 That doesn't work because of the 3 spaces at the start I tried: $txt = str_replace(" ", "", $txt); But it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-468917 Share on other sites More sharing options...
laffin Posted February 17, 2008 Share Posted February 17, 2008 preg_match('@\s*([^\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); \s represents white spaces (spaces tabs newlines) Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-469099 Share on other sites More sharing options...
sean123 Posted February 17, 2008 Author Share Posted February 17, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/91365-grabbing-text-from-a-site-and-adding-to-variables/#findComment-469206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.