Writing to another file

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
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Writing to another file

Post by ChessclubFriend »

From a PHP file I'm writing, I need it to do the following, I need it to insert code inside another .php file. For now, at the very end. Well basically the end, the end of the file is </body></html>. How can I insert data into this other php file right before </body>? Thank you
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

If you're trying to update the content of the page dynamically, you might try approaching the problem using a database if you haven't already looked into it. Otherwise, you'd need to get the file (file_get_contents()), find out where the body and html end tags are in the file using a regular expression (preg_replace()), and then rewrite the file (fwrite()).
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Post by ChessclubFriend »

I had trouble with the syntax for strings like this, this is what I have but it says that b is a modifer etc, what would be the correct syntax for finding the /body tag? thanks

$patterns[0] = '/</body>/';
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You're opening the pattern with a /, and closing with another /. In your pattern you have a / in the middle. Anything after the closing / (the second /, eg the one just before 'body') is taken as a modifier, and b is not a valid option. You need to escape your middle / ... eg

Code: Select all

$patterns[0] = '/<\/body>/';
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You can also change the patter delimiter -- '@' is a common replacement
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Post by ChessclubFriend »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thanks for all your help, I love this Forum.
This is what I have now.

Code: Select all

<?php
$content=file_get_contents("http://www.chessclubfriend.com/Vote.php");
$patterns[0] = '/<\/body>/';
$replacements[0] = "Hello </body>";
if($votetopic)
  {
preg_replace($patterns, $replacements, $content); 
 }
?>
It really looks like it should work to me, but it doesn't. any more ideas?
It should find the </body> tag and replace it with "Hello </body>" But it doesn't, yet. :)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the return value from preg_replace() isn't used, so PHP is doing work that you're choosing to throw away. ;)
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Post by ChessclubFriend »

You were right, I needed the output of preg_replace, Thanks. :)

Code: Select all

$questionNumber = 0;
$questionNumber1 = 1;
$patterns[0] = '/<\/body>/';
$replacements[0] = "This is question # <?php $questionNumber = $questionNumber + $questionNumber1; ?> <BR></body>";
$stuff = preg_replace($patterns, $replacements, $content);

This should start with, "This is question #0" and increment the question number each time I replace the body tag. It says parse error, unexpected '=' in my replacement line or something similar though. How do I enter a php code snip inside my replacement ? :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Seems very odd trying to dynamically adding content like your doing.. Why not simply include() the file, or am I missing something?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

better yet, why not just do $questionNumber + $quesitonNumber1 in your pattern?
Post Reply