[thelist] PHP questions - overwriting and removing spaces

Jay Greenspan jay at trans-city.com
Sun Dec 10 16:09:25 CST 2000


on 12/10/00 3:36 PM, Jason Lustig at lustig at acsu.buffalo.edu wrote:

> OK... I just started learning PHP, and am rewriting my message boards in
> them. So, I have a couple of questions:
> 
> Well, I want to be able overwrite lines in a .txt file. For example, say
> that a file has this in it:
> <snip>
> Eggs
> Chicken
> Milk
> Orange Juice
> </snip>
> and I want to change the "Eggs" to "Apples" without messing with the other
> three lines in the file. This is so taht I can edit posts...


Here's a script that will read a text file, edit it and write out the
contents. Make sure you have permissions to the file you're trying to open.

<?php

$filename = "test.php";
$handle = fopen($filename, "r+");
$contents = fread($handle, filesize($filename));
$contents = str_replace("Eggs", "apples", $contents);
rewind($handle);
fwrite($handle, $contents);
ftruncate($handle,ftell($handle));
fclose($handle);

?>


> 
> Also, for signatures, I am opening up a file in the path
> "members/$username/sig.txt", depending on what the $username is. But what
> the problem is is that, let's say the username is "Help",
> "members/$username/sig.txt" = "members/Help /sig.txt". The problem is the
> space after the username... is there a way to conveniently get rid of the
> space character? The space is always there for some reason. I just want to
> change $username to get rid of the space.

how about trim()

$path = "members/" . trim($username) . "/sig.txt";

This will work if the space is coming from the filename. Are you sure
you haven't inadvertently added a space somewhere in your script?

-j








More information about the thelist mailing list