Question

I am creating a a control panel for minecraft servers(It's a game if you did not not know). I need to process the server.properties file in php to get the online-mode field and get it's value. How do I do this? Any idea?

This is an example of server.properties:

spawn-protection=16
query.port=25565
generator-settings=
force-gamemode=false
allow-nether=true
gamemode=0
enable-query=true
player-idle-timeout=0
difficulty=1
spawn-monsters=true
op-permission-level=4
announce-player-achievements=true
pvp=true
snooper-enabled=true
level-type=DEFAULT
hardcore=false
venable-command-block=false
max-players=5
rcon.port=25575
server-port=25565
debug=false
texture-pack=
server-ip=
spawn-npcs=true
allow-flight=false
level-name=
view-distance=10
resource-pack=
spawn-animals=true
white-list=false
rcon.password=asd
generate-structures=true
online-mode=true
max-build-height=256
level-seed=
enable-rcon=true
motd=A Minecraft Server

And yes, the fields keep juggling around so I can't really predict where the line would be.

Was it helpful?

Solution

The parse ini function would be useful

Example:

$settings = parse_ini_file('server.properties');
echo $settings['level-type']; //will print DEFAULT

OTHER TIPS

If you are using, file_get_contents, then you can use explode to break data from new line.

  $homepage = file_get_contents('http://www.example.com/data.txt');

    $arr=explode("\n",$homepage)

    Alternatively, you can use preg_split and the character group \s which matches every white space character:

    preg_split('/\s+/', $homepage);

Now for searching for particular keyword:

$prop='properties';
$key = array_search($prop, $arr);

Bythis you can find the particular position. now use explode to get the desired result.

$res=explode("=",$key);

if you are able to get the content of the file in a PHP variable using file_get_content() or by some other means and lets say the variable that holds the string is $input_lines

You can use the following regex to get the value

preg_match_all("/online\-mode=(.*)$/m", $input_lines, $output_array);

$output_array will be as

Array
(
    [0] => Array
        (
            [0] => online-mode=true
        )

    [1] => Array
        (
            [0] => true
        )

)

This is how it works

 /online\-mode=(.*)$/m

    online matches the characters online literally (case sensitive)
    \- matches the character - literally
    mode= matches the characters mode= literally (case sensitive)
    1st Capturing group (.*)
        .* matches any character (except newline)
            Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    $ assert position at end of a line
    m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Use explode()

$content = file_get_contents('server.properties');
$lines= explode("\r\n" , $content);


echo '<form name="form1" method="post" action="">';

array_pop($lines);

foreach($lines as $line)
{

     $properties = explode('=' , $line);
     echo $properties[0] . ' = <input type="text" value="'. $properties[1] .'" name="properties[]['. $properties[0] .']"/></br>';
}
echo '<input type="submit" name="submit" value="Submit"></br>';

echo '</form>';

The part above echoes the contents of the file. The part below assembles it. You can put this above the part which is previously mentioned.

if(isset($_POST['properties']))
{

    $new_lines = $_POST['properties'];


    $string = '';

    foreach($new_lines as $line)
    {
        foreach($line as $property => $value)
        {
             $string .= $property . '=' . $value . "\r\n";
        }
    }

    file_put_contents('server.properties' , $string);

    unset($_POST['properties']);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top