Kush Posted February 18, 2011 Share Posted February 18, 2011 I'm trying to extract information from a string that's going to have changing content. There's a possibility that more useless content added to the end of this string, but that stuff doesn't matter. I've put the content I want to extract in bold & underline. hostname: [GE] 24/7 OFFICE N00b Training Facility - GangsterElite.com version : 1.0.0.58/15 4426 secure udp/ip : 74.121.47.242:27016 map : fy_dustdm at: 0 x, 0 y, 0 z players : 0 (20 max) # userid name uniqueid connected ping loss state adr The content I want to extract is going to be different for each time though, but some of the stuff is going to be constant. Here's what would remain constant: hostname: [GE] 24/7 OFFICE N00b Training Facility - GangsterElite.com version : 1.0.0.58/15 4426 secure udp/ip : 74.121.47.242:27016 map : fy_dustdm at: 0 x, 0 y, 0 z players : 0 (20 max) # userid name uniqueid connected ping loss state adr Everything else will be different (need to extract multiple strings of the same format with different content for what I want to extract) so I'm not exactly sure how to handle this. For example, the "hostname" ([GE] 24/7 OFFICE N00b Training Facility - GangsterElite.com) which I want to extract could be anything same with the "map" and "players". I'm guessing that I would have to use regex or split the string somehow, but I've never really been good at that and I absolutely dread it. Help? :'( Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 18, 2011 Share Posted February 18, 2011 Why limit yourself to just those values. Go ahead and parse the entire string. You never know when you may need those other values. Use the following function to return an array of all the parameters (as keys) and their values function getServerData($string) { $data = array( 'hostname' => "~hostname: (.*?) version :~", 'version' => "~version : (.*?) secure udp/ip :~", 'secure ip' => "~secure udp/ip : (.*?) map :~", 'map' => "~map : (.*?) at:~", 'at' => "~at: (.*?) players :~", 'players' => "~players : (.*) #~" ); foreach($data as $varName => $pattern) { preg_match($pattern, $string, $matches); $data[$varName] = $matches[1]; } return $data; } $input = "hostname: [GE] 24/7 OFFICE N00b Training Facility - GangsterElite.com version : 1.0.0.58/15 4426 secure udp/ip : 74.121.47.242:27016 map : fy_dustdm at: 0 x, 0 y, 0 z players : 0 (20 max) # userid name uniqueid connected ping loss state adr"; $serverData = getServerData($input); print_r($serverData); Ouput: Array ( [hostname] => [GE] 24/7 OFFICE N00b Training Facility - GangsterElite.com [version] => 1.0.0.58/15 4426 [secure ip] => 74.121.47.242:27016 [map] => fy_dustdm [at] => 0 x, 0 y, 0 z [players] => 0 (20 max) ) Quote Link to comment Share on other sites More sharing options...
Kush Posted February 18, 2011 Author Share Posted February 18, 2011 I'm reading this string from a socket and apparently it's not working. When testing and using it as a string like you did in the actual script like: $input = "hostname: Hooter's GG 5.1 Elimination HLstatsX:CE version : 1.0.0.58/15 4426 secure udp/ip : 25.2.132.39:27015 map : fy_dustdm at: 0 x, 0 y, 0 z players : 0 (20 max) # userid name uniqueid connected ping loss state adr "; It works, but when I pass in what comes back from the socket it returns with an empty array: Array ( [hostname] => [version] => [secure ip] => [map] => [at] => [players] => ) I do a var dump and this is what comes back: string(229) "hostname: Hooter's GG 5.1 Elimination HLstatsX:CE version : 1.0.0.58/15 4426 secure udp/ip : 25.2.132.39:27015 map : fy_dustdm at: 0 x, 0 y, 0 z players : 0 (20 max) # userid name uniqueid connected ping loss state adr " Is there a way I can make it a string like the way I did in the script? Quote Link to comment Share on other sites More sharing options...
silkfire Posted February 18, 2011 Share Posted February 18, 2011 Then something else comes back from the socket. Could you try to just echo that string and then view source and paste the entire string here? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 18, 2011 Share Posted February 18, 2011 Is there a way I can make it a string like the way I did in the script? According to your vardump it is a string. Let's do some debugging to see what may be the problem. Add this line just before the return in the function I provided. $data['string'] = $string; Then do a print_r() of the return value. If the 'string' index has no value then you are passing an empty string into the function. If it shows the expected data, then it may be that some of the "spaces" in that string aren't really spaces. There are several "white-space" characters that may look like spaces in the rendered code. When you did the vardump did you look at the HTML source code or only the result displayed in the browser??? There may be multiple spaces, linebreaks, tabs, etc. in that string. But, when rendered in a browser they are reduced to a single space. Do a vardump again and check the HTML source code and post what is there. Quote Link to comment 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.