2013-05-03 27 views
1

我想用replaceword.txt文件替換searchword.txt文件中的一些字符串,但用我的PHP代碼它只能替換前兩個字符串,我想替換文件中的所有字符串。str_replace在file.txt中的字符串與另一個file.txt

這裏是我的PHP代碼:

$txt_s = file_get_contents("searchword.txt"); 
$txt_s = explode(";", $txt_s); 
$txt_r = file_get_contents("replaceword.txt"); 
$txt_r = explode(";", $txt_r); 
$term = str_replace($txt_s, $txt_r, $last_tmp_); 

這裏是我的searchword.txt文件看起來像:

hello; modern; corn; banana; apple; 

這裏是我的replaceword.txt文件看起來像:

goodbye; fashionable; popcorn; monkey; sweet; 

我怎樣才能取代所有的字符串,不只是前兩個字符串?或者可能是任何不同的方式來做到這一點,請讓我知道。

+0

我測試你的代碼,它的工作原理以及在我結束。嘗試'str_ireplace'不敏感的情況。看到[這裏](http://php.net/manual/en/function.str-ireplace.php) – ROMMEL 2013-05-03 20:39:35

回答

2

您必須去除例如空格與array_map()

$newarray = array_map('trim', $array); 

用途:

$txt_s = file_get_contents("searchword.txt"); 
$txt_s = array_map('trim', explode(";", $txt_s)); 
$txt_r = file_get_contents("replaceword.txt"); 
$txt_r = array_map('trim', explode(";", $txt_r)); 
$term = str_replace($txt_s, $txt_r, $last_tmp_); 
+0

哇,非常感謝,它的作品.. – RK26 2013-05-03 20:38:27