PhP使用爆炸和替换大文件中的字符串

I'm very very new to PHP so any help will do :-)

I want to change -- to : in a large file. What have I done wrong?

$handle = fopen('april.log.txt', 'r');

while  (!feof($handle)){
 $line = fgets($handle, 1024);
 $to_replace =array('--',':');
 $clean = str_replace($to_replace,':',$line);
 echo $line;
}

You are printing the old string:

echo $line;

You need to print the modified string:

echo $clean;

And you can change your $to_replace to:

$to_replace = '--';

Try this:

<?php
$handle = fopen('yourfile.txt', 'r');

while  (!feof($handle)){
 $line = fgets($handle, 1024);
 $line = str_replace('--',':',$line);
 echo $line;
}

2 things:

  • You don't need to have your search-replace in an array.
  • You assigned the result of str_replace in $clean instrad of $line