Solved

opening a text file and writing contents to new filename

Posted on 2003-12-01
8
227 Views
Last Modified: 2008-03-06
I need to open up a text file and then write the contents of that text file into a different file. Inside the text file there are some variables that need the values printed out in the new file. The Original file is merely a template and the other files have certain filenames changed accordingly. Does that make sens? I hope so.

--Rex
0
Comment
Question by:rexburgess
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9856163
hi,

i don't quite understand what you're trying to do but i suppose it's something like this:

<?php
$readfilename = "file.txt"; // change this
$writefilename = "fileout.txt"; // change this too
$file = file_get_contents($readfilename);
eval("\$file = \"$file\";");
$fp = fopen($writefilename, "wb");
if(!$fp) {
  print "Error in opening file";
} else {
  $write = fwrite($fp, $file);
  if($write) {
    print "Successfully written to file";
  } else {
    print "Error writing to file";
  }
}
?>

tell me how you get on

loz
0
 
LVL 1

Expert Comment

by:Snyke
ID: 9860426
Try doing this:
<?php
  $readfilename = "file.txt";
  $writefilename = "fileout.txt";
  $fd = fopen($writefile) or die("Could not open file.");
  $indata = file($readfile) or die("Could not open file.");
  foreach($indata as $curline){
    $outdata[] = preg_replace("/%([^ ]+)%/",$indata,$${1}); // <-- Notice the 2 $$ are wanted.
    fwrite($fd,$outdata);
  }
?>
Notice that I've never tried to do the $$ stuff but I heard about it some time ago and ou seem to need it.
Otherwise just define a preg_replace for every variable you want to insert in the file.
0
 

Author Comment

by:rexburgess
ID: 9862766
Alright, this is what I came up with and it works like a charm.

---------------------------------------------------------------------------
lot.php  script
---------------------------------------------------------------------------

<?PHP
include("global.php");

//open template file
$contents=file_get_contents("$full_file_path/lot.txt");

//replace variables
$contents2=ereg_replace("VARIABLE_LOT_NUMBER", "$lot", "$contents");
$filename_template=ereg_replace("VARIABLE_PLAN_NUMBER", "$plan", "$contents2");

//create new file
touch("$full_file_path/lot$lot_number.html");

//chmod new file
chmod("$full_file_path/lot$lot_number.html", 0777);

//open new file for writing
$filename_final=fopen("$full_file_path/lot$lot_number.html", "w")
  or die("<center>ERROR: Cannot open desired lot. Please try again later.</center>");
 
//write to new file
fputs($filename_final,$filename_template);
?>

---------------------------------------------------------------------------
end  lot.php  script
---------------------------------------------------------------------------


--Rex
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:rjdown
ID: 9880436
Might I just add a note here:

according to the php manual:

"preg_replace(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg_replace()."

Furthermore, str_replace() should be used in this case. Once again from the PHP manual:

"If you don't need fancy replacing rules, you should always use this function instead of ereg_replace() or preg_replace()."

I'm being fussy I know, but good habits make good scripts...
0
 
LVL 1

Expert Comment

by:Snyke
ID: 9881648
Yes that's right.
But I noticed that doing it your way, rexburgess, replaces just one kind of strings. If you have more than one substitutions defined in lot.txt you will have to do a foreach that cycles through the substitutions.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12623933
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google Dork query 7 70
MySqli Real Escape String and SQL Injection 1 50
htaccess block all countries but the US 4 44
How to get chosen background-color on every line? 10 21
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question