Writing to another file
Moderator: General Moderators
-
ChessclubFriend
- Forum Newbie
- Posts: 21
- Joined: Sun Mar 25, 2007 9:13 pm
Writing to another file
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
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
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
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>/';-
ChessclubFriend
- Forum Newbie
- Posts: 21
- Joined: Sun Mar 25, 2007 9:13 pm
feyd | Please use
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]
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 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]-
ChessclubFriend
- Forum Newbie
- Posts: 21
- Joined: Sun Mar 25, 2007 9:13 pm
You were right, I needed the output of preg_replace, Thanks. 
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 ?
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 ?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: