[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

opening a text file and writing contents to new filename

Posted on 2003-12-01
8
Medium Priority
?
248 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

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!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

656 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