I have tried to make a small script that will read a txt file and replace certain words with a number and then display the final result. Its not working as I planned and if anyone know how to do this could I have it create a output.txt file with the all changes in it? Here is what I have so far. <?php $file = './3/test.txt'; $words = array("United Kingdom", "United States", "Canada"); $numbers = array("1", "2", "3"); $content = '$file'; $change = str_replace($words, $numbers, $content); Print $change; ?> PHP: I tried to use a line like this $phrase = fopen($file, "r"); PHP: But then I get this displayed on the page So not sure if it is trying to work or not the txt file is 3.36mb in size
use $phrase = fopen($file, "w"); PHP: instead. then $content = $file; PHP: (qoute removed), I hope it helps.
I would do: <?php $file = './3/test.txt'; $words = array("United Kingdom", "United States", "Canada"); $numbers = array("1", "2", "3"); $content = file_get_contents($file); $change = str_replace($words, $numbers, $content); print $change; ?> PHP: Many people say that fopen(), then fread() is faster than file_get_contents() but its easier to understand the latter.