1

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; } 
1
  • Why do you need to replace already-existing : characters with :? Commented Nov 5, 2011 at 17:26

2 Answers 2

5

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 = '--'; 
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.