2012-08-10 11 views
21

テキストファイルを開いて文字列を置き換える必要があります。私はこれが必要ですPHPを使用してテキストファイルに文字列を置き換えます。

Old String: <span id="$msgid" style="display: block;"> 
New String: <span id="$msgid" style="display: none;"> 

これはこれまで私が持っていたものですが、余分な空白の他にテキストファイルに変更はありません。

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

どうすればいいですか?あなたのコメントへ

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

msghistory.txtファイルの書き込み権限 – Lobo

+0

これは正しいですか? '$ deletedFormat =" "';' –

+0

構文エラーがあります。 '$ deletedFormat =" "';'あなたは余分な一重引用符を持っています。 –

答えて

59

は、この作業を行います。これは、高速かつ正確な魅力、同じように動作し

/** 
* 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; 
} 
+1

$ oldMessageの代わりに "$ oldMessage"を書く理由はありますか?引用符に何らかの目的があるかどうかはわかりません。 – user1111929

+0

U R素晴らしい人。 – ako

3

::私はそれが起こるときにエラーメッセージを与える機能を作った

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

使用法:

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

//文字列解析に関する解読について決して忘れないでください //強力で高速なツールです

+0

が爆発し、爆縮は単純なstr_replaceよりも遅い:http://micro-optimization.com/str_replace-vs-implode-explode.html –

関連する問題