2014-03-28 202 views
0

我正在爲我的世界服務器創建一個控制面板(如果您不知道,這是一款遊戲)。我需要在php中處理server.properties文件以獲取在線模式字段並獲取它的值。我該怎麼做呢?任何想法?從文件中獲取一行並分割成一個數組

這是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 

是的,該領域保持周圍雜耍,所以我真的不能預測哪裏行會。

+0

所以是U能夠得到這些文件的內容在一個變量的方法及其這裏 ? –

+0

查看我的回答我更新了它。由於您沒有正常名字,我無法引用您。 – Rimble

回答

1

解析INI功能將是有益的

例子:

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

如果您使用的是file_get_contents,那麼您可以使用爆炸來從新行中分解數據。

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

現在用於搜索特定的關鍵字:

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

Bythis你可以找到具體的位置。 現在使用爆炸來獲得所需的結果。

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

使用爆炸()

$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>'; 

以上的部分相呼應的文件的內容。 下面的部分組裝它。你可以把它放在前面提到的部分上面。

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']); 

} 
+0

@ user3001689解決您的問題。你將不得不在CSS和HTML做樣式我不會那樣做。並確保文件具有正確的權限。 – Rimble

1

,如果你能得到文件的內容,使用file_get_content() PHP變量或通過其他手段,讓說,包含字符串變量爲$input_lines

您可以使用下面的正則表達式拿到價值

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

$ output_array會像

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

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

) 

這是它如何工作的

/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) 
相關問題