Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Php Writes Into Txt File, a question
kakingho
post Jul 17 2006, 04:34 PM
Post #1


Member [Level 1]
****

Group: Members
Posts: 52
Joined: 17-July 05
From: hong kong
Member No.: 9,520



QUOTE
<?php

$fp=fopen("test.txt","a");
fputs($fp,"ok");
?>


The above method is only to add "ok" at the end (DO NOT write on a new line)

I want to know...
How to add data on a new line and at the end of txt file??!

thx~
Go to the top of the page
 
+Quote Post
electriic ink
post Jul 17 2006, 07:29 PM
Post #2


Incest is a game the whole family can play.
Group Icon

Group: [MODERATOR]
Posts: 1,223
Joined: 11-February 05
From: Heaven
Member No.: 3,709



It would be :

CODE
<?php

$fp = fopen ("test.txt", "a");
          fputs ($fp, "ok \nnewline");
?>


So yeah, basically, you just add \n but the one things I've found is that the \n must be in " NOT '
Go to the top of the page
 
+Quote Post
galexcd
post Jul 18 2006, 02:28 AM
Post #3


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,074
Joined: 25-September 05
From: Los Angeles, California
Member No.: 12,251



I have even noticed that in php strings can acctualy have a real carrage return! Instead of the \n u can just make the string on two lines! Sorta figured that one out on accident.

Example:
CODE
<?php

$fp = fopen ("test.txt", "a");
          fputs ($fp, "ok
newline");
?>


This post has been edited by galexcd: May 13 2008, 03:31 AM
Go to the top of the page
 
+Quote Post
electron
post Jul 18 2006, 06:36 AM
Post #4


Premium Member
********

Group: Members
Posts: 162
Joined: 10-May 06
Member No.: 23,375



Here is a better File writing function :

CODE
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        exit;
   }

   // Write $somecontent to our opened file.
   if (fwrite($handle, $somecontent) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
  
   echo "Success, wrote ($somecontent) to file ($filename)";
  
   fclose($handle);

} else {
   echo "The file $filename is not writable";
}
?>


Also you should use the \n thing to hit enter in a file.
But i dont think so it is necessary to put it in "" double quotes rather than single quotes .

Best of luck
Go to the top of the page
 
+Quote Post
kakingho
post Jul 18 2006, 02:29 PM
Post #5


Member [Level 1]
****

Group: Members
Posts: 52
Joined: 17-July 05
From: hong kong
Member No.: 9,520



..thx a lot~~

successful!
Go to the top of the page
 
+Quote Post
electron
post Jul 19 2006, 04:23 AM
Post #6


Premium Member
********

Group: Members
Posts: 162
Joined: 10-May 06
Member No.: 23,375



You can also find a way to modify a existing file to change some of its values.

e.g. If you have a global file that controls all your site settings and would like to modify it via the Admin Center you can use str_repalce function to do so.

It is very effective for users.

Good luck
Go to the top of the page
 
+Quote Post
Florisjuh
post Jul 19 2006, 04:10 PM
Post #7


Proud to be hosted
*********

Group: Members
Posts: 992
Joined: 11-July 04
From: NL
Member No.: 75



Thanks electron for the nice and very good usable PHP script, keep them coming tongue.gif )
Go to the top of the page
 
+Quote Post
electron
post Jul 20 2006, 02:54 AM
Post #8


Premium Member
********

Group: Members
Posts: 162
Joined: 10-May 06
Member No.: 23,375



What type of code or function do youy want ?

I can help you.

By the way you can also use preg_replace() fucntion if you are not so sure of the TEXT MATTER but no the Format of it e.g. a Hyperlink.

But if you know the text then its advisable to use str_replace() function.

Also for inserting any kind of quote - single or double - use a backslash to write it in the file.

I love PHP
Go to the top of the page
 
+Quote Post
Spectre
post Jul 20 2006, 03:15 PM
Post #9


Privileged Member
*********

Group: Members
Posts: 873
Joined: 30-July 04
Member No.: 246



A like PHP. I wouldn't go so far as to say I love it.

Just a little expansion and clarification. preg_replace() uses the Perl-compatible regular expression engine built into PHP (as do all preg_ functions) to perform a regex search and replace operation on a given string, where the subject is searched for a pattern matching that given and replaced with the specified replacement (which can contain backreferences to the pattern matched). str_replace() is just a very simple static search and replace function for strings, where it replaces all given instances of one string within another with a specified value. preg_replace() is so much more powerful due to its harnessing PCRE - however, this means it is also slower, and should not be used unless required. Quotes only need to be escaped when that exact character is being used as a string delimiter. For instance, the string 'a"b"c' is perfectly legal, without the double quotes being escaped, as single quotes are acting as string delimiters - however, were the double quotes single quotes instead, they would need to be escaped, for example: 'a\'b\'c'. Within single quotes, no other characters will ever be escaped other than single quotes - so '\n' remains just that, whereas "\n" is the equivalent of chr(10) (and "\r" the equivalent of chr(13)). Variables will also be substituted within double quotes - eg. "the value is $variable" will become "the value is x" (assuming x is the value of the variable $variable - if that makes any sense at all). Because of these two factors, it is better to use single quotes where possible, even if this means concatenating strings delimited via two different sets of quotes (eg. 'start' . "\n" . 'end'), as it will always be faster. Although the speed difference generally isn't noticable, it can begin to slow down if large amounts of text is being processed, or if it is happening in rapid succession (eg. the code behind a very popular website), and it's simply a good programming practice to write clean, fast and efficient code.

So anyway, that's my two and half cents worth.