I need help with file editing

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
DraginX
Forum Newbie
Posts: 2
Joined: Mon Apr 21, 2003 12:52 pm

I need help with file editing

Post by DraginX »

I was just wondering how could I take a file, and if it has text in it, I want to delete that text. Then I want to put in new text in.

(Im making a little MySQL tihngy, with menu tree and I need to edit the menu tree file to display the correct information).

Thank you :)
phpfreak
Forum Commoner
Posts: 30
Joined: Fri Mar 21, 2003 10:28 am
Location: New Jersey,USA
Contact:

fopen date escape character regular expression onload php

Post by phpfreak »

hi there,

the below code deletes the contents in the text file and then inserts the date into it.

==============================================
<?php
$first=open_file();
function open_file()
{

$for_date = date("H:i-j/m/y");
$fp = fopen ("c:\\sesha\\info.txt", "w");
//change the file to any text file you want.
//do use the escape character ''' in the windows.
if (!($fp))
{
exit;
}
while (!feof($fp))
{
$line = fread ( $fp, 1024 );
str_replace ('[a-zA-z0-9]+',"",$line);
}


fputs($fp," ");
fputs ($fp,"$for_date|$sh");

fclose ($fp);

}


?>
<body onLoad="<?php echo $first;?>">

===============================================
hope it helped

regards
srinivas
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Have you looked at the Manual's filesystem functions? fopen and it's associated funcs should help you out. :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Minor suggestion, change:

$fp = fopen ("c:\\sesha\\info.txt", "w");

to..

$fp = fopen ("c:\\sesha\\info.txt", "wb");


Php Manual:

Note:
The mode may contain the letter 'b'. This is useful only on systems which differentiate between binary and text files (i.e. Windows. It's useless on Unix). If not needed, this will be ignored. You are encouraged to include the 'b' flag in order to make your scripts more portable.
Post Reply