Page 1 of 1

Text to/from a file.

Posted: Fri Oct 18, 2002 12:28 pm
by sinewave
I am looking for some sort of a tutorial to open a file and display the entire contents of it. This should be easy i just havent found much telling me what i need.

Also, when editing a file is there a way to change multiple spaces to  

example:
"he ll o" would output "he ll o"
so i would want the file to be saved as

"h     ll   o"

any easy way for that also?

Posted: Fri Oct 18, 2002 12:59 pm
by haagen
If you're on unix you could display the contents of a file with the following command:

passthru("cat filename");

Posted: Fri Oct 18, 2002 1:02 pm
by sinewave
hmmm kinda needs to be for win and unix

Posted: Fri Oct 18, 2002 1:14 pm
by haagen
Try this (untested)

Code: Select all

$fp = fopen("filename", r);

if($fp){
    $cont = fread($fp, filesize("filename"));
    flcose($fp);
}

echo $fp;
For the other part of you question checkout the strtr-funktion.

Posted: Fri Oct 18, 2002 1:16 pm
by haagen
Darn, :oops:

it shold be like this;

Code: Select all

$fp = fopen("filename", "r");

Posted: Mon Oct 21, 2002 4:36 am
by twigletmac
To replace all spaces with   use str_replace():

Code: Select all

$non_breaking_spaces = str_replace(' ', ' ', $normal_spaces);
When working with a file you can also use file() to read it into an array and foreach() to loop through the array and echo each line.

http://www.php.net/manual/en/ref.filesystem.php

Mac