2017-02-20 50 views
0

我正在用file_get_contents读取文件。 有些行可以有多个“=”字符,我想删除这些行。删除存在多个字符的行

我试图

str_replace("=", "", $content); 

但这种替代的“=”,而不是删除这些行所有出现。

有什么想法吗?

UPDATE:从文件我的内容是这样的:

something 

apple is =greee= maybe red 
sugar is white 

sky is =blue 
+1

你能告诉我们一些有关你的线条的例子吗? – JustBaron

+0

谢谢,我从文件 – peter

+0

添加样本内容检查答案 – JustBaron

回答

0

没有看到你的文件/字符串的一个例子,这是一个有点棘手的提醒,但基本原理我会工作到会是这样的这样的:

$FileName = "PathToFile"; 
$FileData = file_get_contents($FileName); 
$FileDataLines = explode("\r\n", $FileData); // explode lines by ("\n", "\r\n", etc) 

$FindChar = "="; // the character you want to find 

foreach($FileDataLines as $FileDataLine){ 
    $NoOfChar = substr_count($FileDataLine, $FindChar); // finds the number of occurrences of character in string 
    if($NoOfChar <= 1){ // if the character appears less than two times 
     $Results[] = $FileDataLine; // add to the results 
    } 
} 
# print the results 
print_r($Results); 
# build a new file 
$NewFileName = "YourNewFile"; 
$NewFileData = implode("\r\n", $Results); 
file_put_contents($NewFileName, $NewFileData); 

希望它可以帮助

+0

谢谢,我从文件 – peter

+0

添加示例内容试试这个答案,它应该删除字符串'apple is = greee =也许红色' – JustBaron

+0

谢谢@justbaron,这正是我想要做的! :) – peter