2015-05-06 18 views
0

我有一個文件,包含:
file content NULNULNULNUL something else
當我嘗試獲取文件內容
$str = file_get_contents($fileName),在$str只出現file content
如何在不打開文件的情況下替換文件中的NUL?PHP替換文件的性質,而不打開文件

+11

我怎樣才能得到我的巧克力從我的冰箱沒有打開它?! (< - 如果你能回答這個問題,你也可以回答你自己的問題) – Rizier123

+0

好的,我如何獲得數據,如果在文件的中間,我有NUL? – igorb0214

+1

它應該工作正常。 'file_get_contents()'將一個文件讀入一個字符串。所以你必須向我們展示你的**完整**和**真實**代碼。告訴我們目前的產出是什麼以及你期望的結果 – Rizier123

回答

0

1.)將替換的內容作爲字符串獲取。首先獲取文件內容,就像你一樣。

$str = file_get_contents($fileName) 

然後像一個空格代替NUL所有出現:

$new_content = str_replace('NUL'," ",$str); 

OR

2)要替換的文件裏面的內容(更換內容,並把它寫回到檔案):

file_put_contents($fileName,str_replace('NUL',' ',file_get_contents($fileName))); 

這裏我們使用的是file_put_contents - 它將一個字符串寫入文件。我們通過filenamereplaced-string作爲參數

-1

我將每個字符assci代碼。這是現在的工作:

$newStr=''; 
$str = file_get_contents($fileName); 
$length = strlen($str); 

for ($i=0; $i < $length; $i++) 
{ 
    $current = ord($str{$i}); 
    if ($current != 0x0) 
    { 
     $newStr .= chr($current); 
    } 
    else 
    { 
     $newStr .= ""; 
    } 
} 
$num = file_put_contents($fileName,$newStr);