2012-08-10 17 views
21

Bir metin dosyası açıp bir dizeyi değiştirmem gerekiyor. Ben Bu defa ne var ama, fazladan beyaz boşluk yanında metin dosyasında herhangi bir değişiklik görmüyorum buPHP'yi kullanarak metin dosyasındaki dizgiyi değiştirin

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

gerekir.

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

Nasıl yapabilirim? Yorumlarınızı

$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 dosyasında yazma izinleri – Lobo

+0

Bu doğru mu? '$ deletedFormat =" "';” –

+0

Bir sözdizimi hatası var. '$ deletedFormat =" "';' ekstra bir tek teklifiniz var. –

cevap

59

bu işi yapar. Bu hızlı ve doğru bir çekicilik gibi çalışır

/** 
* 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 yerine "$ oldMessage" yazmanız için bir neden var mı? Alıntıların burada herhangi bir amacı olup olmadığından emin değilim. – user1111929

+0

U R harika adam. – ako

3

: Bu olduğunda bir hata mesajı vermek bir işlev yaptık

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

Kullanımı:

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

// // dize ayrıştırma hakkında geldiğinde EXPLODE unutma asla güçlü ve hızlı bir araç

+0

patlayabilir, implodlar basit bir str_replace'den daha yavaştır: http://micro-optimization.com/str_replace-vs-implode-explode.html –

İlgili konular