2012-08-10 12 views
21

Potrzebuję otworzyć plik tekstowy i zamienić ciąg znaków. To jest to, co mam do tej pory, ale nie widzę żadnych zmian w tekście oprócz dodatkowych białych spacji.Zastąp ciąg w pliku tekstowym przy użyciu PHP

$msgid = $_GET['msgid']; 

$oldMessage = ""; 
$deletedFormat = ""; 

// Read the entire string 
$str = implode("\n", file('msghistory.txt')); 

$fp = fopen('msghistory.txt', 'w'); 

// Replace something in the file string - this is a VERY simple example 
$str = str_replace("$oldMessage", "$deletedFormat", $str); 

fwrite($fp, $str, strlen($str)); 
fclose($fp); 

Jak mogę to zrobić?

+0

Upewnij się, że masz pisz uprawnienia do pliku msghistory.txt – Lobo

+0

Czy to prawda? '$ deletedFormat =" "';' –

+0

Masz błąd składniowy. '$ deletedFormat =" "';' masz dodatkowy pojedynczy cudzysłów. –

Odpowiedz

59

to działa:

$msgid = $_GET['msgid']; 

$oldMessage = ""; 

$deletedFormat = ""; 

//read the entire string 
$str=file_get_contents('msghistory.txt'); 

//replace something in the file string - this is a VERY simple example 
$str=str_replace("$oldMessage", "$deletedFormat",$str); 

//write the entire string 
file_put_contents('msghistory.txt', $str); 
+1

Czy istnieje powód, dla którego piszesz "$ oldMessage" zamiast $ oldMessage? Nie jestem pewien, czy cytaty mają tutaj jakiś cel. – user1111929

+0

U R niesamowity człowiek. – ako

8

Dzięki komentarze. Zrobiłem funkcję, która daje komunikat o błędzie, gdy to się dzieje:

/** 
* Replaces a string in a file 
* 
* @param string $FilePath 
* @param string $OldText text to be replaced 
* @param string $NewText new text 
* @return array $Result status (success | error) & message (file exist, file permissions) 
*/ 
function replace_in_file($FilePath, $OldText, $NewText) 
{ 
    $Result = array('status' => 'error', 'message' => ''); 
    if(file_exists($FilePath)===TRUE) 
    { 
     if(is_writeable($FilePath)) 
     { 
      try 
      { 
       $FileContent = file_get_contents($FilePath); 
       $FileContent = str_replace($OldText, $NewText, $FileContent); 
       if(file_put_contents($FilePath, $FileContent) > 0) 
       { 
        $Result["status"] = 'success'; 
       } 
       else 
       { 
        $Result["message"] = 'Error while writing file'; 
       } 
      } 
      catch(Exception $e) 
      { 
       $Result["message"] = 'Error : '.$e; 
      } 
     } 
     else 
     { 
      $Result["message"] = 'File '.$FilePath.' is not writable !'; 
     } 
    } 
    else 
    { 
     $Result["message"] = 'File '.$FilePath.' does not exist !'; 
    } 
    return $Result; 
} 
3

To działa jak czar, szybki i dokładny:

function replace_string_in_file($filename, $string_to_replace, $replace_with){ 
    $content=file_get_contents($filename); 
    $content_chunks=explode($string_to_replace, $content); 
    $content=implode($replace_with, $content_chunks); 
    file_put_contents($filename, $content); 
} 

Zastosowanie:

$filename="users/data/letter.txt"; 
$string_to_replace="US$"; 
$replace_with="Yuan"; 
replace_string_in_file($filename, $string_to_replace, $replace_with); 

// nigdy nie zapomnij o EXPLODE, jeśli chodzi o parsowanie ciągów // to potężne i szybkie narzędzie

+0

eksplodować, implodes są wolniejsze niż proste str_replace: http://micro-optimization.com/str_replace-vs-implode-explode.html –

Powiązane problemy