Text to/from a file.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Text to/from a file.

Post 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?
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post by haagen »

If you're on unix you could display the contents of a file with the following command:

passthru("cat filename");
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post by sinewave »

hmmm kinda needs to be for win and unix
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post 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.
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post by haagen »

Darn, :oops:

it shold be like this;

Code: Select all

$fp = fopen("filename", "r");
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply