2017-07-22 30 views
0

我從shell_exec輸出看起來像這樣:並非所有的文字被寫入到文件

0134d690 W ContainerSetContentPacket::getName() const     
00d06f64 W ResourcePackDataInfoPacket::getId() const     
00d06f80 W ResourcePackDataInfoPacket::write(BinaryStream&) const  
00d0713c W ResourcePackDataInfoPacket::handle(NetworkIdentifier const&, 
NetEventCallback&) const            
00d06f68 W ResourcePackDataInfoPacket::getName() const     
00bf510c W StructureBlockUpdatePacket::getId() const     
00bf5128 W StructureBlockUpdatePacket::write(BinaryStream&) const  
00bf52f0 W StructureBlockUpdatePacket::handle(NetworkIdentifier const&, 
NetEventCallback&) const 

和我想要寫的方法與類名的文件。如果你不知道我在說什麼,下面有一個例子:

ClassName::function() 
ClassName::function2() 

我還想寫function()function2()到一個名爲ClassName.txt文件。現在我的代碼現在創建了所有文件並只添加了一個函數(getName())或上面的示例function()但不是function2()

現在,這是一個問題。在寫入文件之前,當我在下面回顯$method變量時,它顯示所有功能,但只寫入一個文件。

PHP代碼:

<?php 

$out = trim(shell_exec("nm -DC *.so | grep 'Packet::'")); 
$out = explode("\n", $out); 
$out = array_filter($out, "trim"); 
foreach($out as $line) { 
    $line = explode(' ', $line, 2); 
    $class = substr($line[1], 0, strpos($line[1], "::")); 

if(strpos($class, "std") === false and strpos($class, "void") === false 
    and strpos($class, "vtable") === false) { 

    $title = array_filter(explode("\n", substr($class, 2)), "trim"); 
} 

foreach($title as $name) { 
    $function = substr($line[1], strpos($line[1], "::")); 
    $function = substr($function, 0, strpos($function, ")")); 

    if(strpos($function, "_") === false and strpos($function, "<") === false 
     and strpos($function, "+") === false 
     and strpos($function, "vtable") === false 
     and strpos($function, "void") === false) { 

     $method = str_replace("W", "", "void $function);"); 
     $method = str_replace("T", "", $method); 
     $method = str_replace("::", "", $method); 
    } 
    file_put_contents("stuff/$name.txt", $method . PHP_EOL);  
} 

}

回答

0

當你調用file_put_contents,該文件將被覆蓋。因此,您正在寫入文件,然後每次循環迭代時覆蓋該文件。

您可以使用FILE_APPEND作爲第三個參數file_put_contents告訴它附加到文件,而不是,但在這種情況下,它可能更有意義,首先fopen(),然後fwrite()在循環中的每一次迭代,然後調用fclose()在結束。這樣該文件只打開一次,而不是循環的每次迭代。

file_put_contents("stuff/" . $name . ".txt", $method . PHP_EOL, FILE_APPEND); 
相關問題