Jump to content

Recommended Posts

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?  :'(

Link to comment
https://forums.phpfreaks.com/topic/228047-extracting-content/
Share on other sites

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)
)

Link to comment
https://forums.phpfreaks.com/topic/228047-extracting-content/#findComment-1175920
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/228047-extracting-content/#findComment-1175976
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/228047-extracting-content/#findComment-1176337
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.