So i have created this code that is grabing info from a server, the string that it is giving me has these specail charecters that i need to get rid of, then I need to exploade it by another.

Here is my code:

function fetch_server_info($ip, $port){
$socket = @fsockopen($ip, $port, $errno, $errstr, 0.5);

if($socket === false){
return false;
}

fwrite($socket, "\xfe");

$data = fread($socket, 256);

if(substr($data, 0, 1) != "\xff"){
return false;
}

$data = mb_convert_encoding(substr($data, 3), 'utf-8', 'UCS-2');

var_dump($data);

/*return array(
'motd' => $data[0],
'players' => intval($data[1]),
'max_players' => intval($data[2]),
);*/
}

and here is the string that i am geting: string(46) "§4Mine§bMad §4Multi-World §f[1.5.1]§3§15"

I need to take out the Â's the 4's the lower case b and f and i need to explode it by the §, but I know how to explode it bye that but first I need to take out the other stuff.

It looks like an encoding issue have you tried it with out the encoding. Thats my guess but i don't much work with encoding so i don't know what you even need the encoding for.

Member Avatar for diafol
header('Content-Type: text/html; charset=utf-8');

$str = "§4Mine§bMad §4Multi-World §f[1.5.1]§3§15";

$str2  = str_replace(array("§4","§b","§f","§"),"§",$str);

$bits = array_filter(array_map("trim", explode("§",$str2))); //take off array functions if you don't need them

print_r($bits);

Justa simple replace and explode

I should note that the srting actualy updates its self so if I use the string has hard code it wouldent work how need it, second being that I orgianaly had it exploding the § and so O needed the

/*return array(
'motd' => $data[0],
'players' => intval($data[1]),
'max_players' => intval($data[2]),
);*/

because I was origanaly trying to grab §4Mine§bMad §4Multi-World §f[1.5.1] for the motd and then the §3§15 for the players in max players but when I saw that it at other charecters I relized that the other charecters have to come out first befor I explode the

Here's my orgional code:

    function fetch_server_info($ip, $port){
    $socket = @fsockopen($ip, $port, $errno, $errstr, 0.5);
    if($socket === false){
    return false;
    }

    fwrite($socket, "\xfe");
    $data = fread($socket, 256);
    if(substr($data, 0, 1) != "\xff"){
    return false;
    }

    $data = explode('§',mb_convert_encoding(substr($data, 3), 'utf-8', 'UCS-2'));

    var_dump($data);

    return array(
    'motd' => $data[0],
    'players' => intval($data[1]),
    'max_players' => intval($data[2]),
    );
    }

so wouldent I be able to do this to clean up the code

$data = mb_convert_encoding(substr($data, 3), 'utf-8', 'UCS-2');

$str = $data;

$str2 = str_replace(array("§4","§b","§f","§"),"§",$str);

$bits = array_filter(array_map("trim", explode("§",$str2)));

var_dump($bits);

then return the aray and set the information to what i need being like motd => $bits[0][1][2][3]

Member Avatar for diafol

You give no info about which array items relate to the ones that you want.
Yes I used a hard-coded string, but you just substitute that for a dynamic one.

WHy are you converting the encoding??

If you give an example of the original string, indicating which bits you need extracting perhaps that would help.

Are the 4, b, f always conencted to the § ?

More info please.

Ok first why im converting the encoding is because the server im connecting to is java based and instead of geting this §4Mine§bMad §4Multi-World §f[1.5.1]§3§15 i get this, ÿ(§4Mine§bMad §4Multi-World §f[1.5.1]§4§15. Seconed the orgianal string I had befor I took off the explode and and aray funcion was this, array(7) { [0]=> string(1) "Â" [1]=> string(6) "4MineÂ" [2]=> string(6) "bMad Â" [3]=> string(14) "4Multi-World Â" [4]=> string(9) "f[1.5.1]Â" [5]=> string(2) "5Â" [6]=> string(2) "15" }. Finaly yes the 4 b f is always conected to the § but lets say the server changes the color of what im grabing the 4 b f might change.

If you want all of my code to see whats going on I can add it.

ok this is my code, it worked, thank you thank you thank you.

function fetch_server_info($ip, $port){
$socket = @fsockopen($ip, $port, $errno, $errstr, 0.5);

if($socket === false){
return false;
}

fwrite($socket, "\xfe");

$data = fread($socket, 256);

if(substr($data, 0, 1) != "\xff"){
return false;
}

$data = mb_convert_encoding(substr($data, 3), 'utf-8', 'UCS-2');

$str = $data;

$str2 = str_replace(array("§4","§b","§f","§"),"§",$str);

$bits = array_filter(array_map("trim", explode("§",$str2)));

return array(
'motd' => $bits[0][1][2][3][4],
'players' => intval($bits[5]),
'max_players' => intval($bits[6]),
);

but my next issue is that in the motd => $bits it wont show all that i want, unless im forgeting to parse it with a . or . $bits

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.