Page 1 of 1
Writing to another file
Posted: Wed Apr 11, 2007 3:06 am
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
Posted: Wed Apr 11, 2007 3:36 am
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()).
Posted: Wed Apr 11, 2007 4:43 am
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>/';
Posted: Wed Apr 11, 2007 4:57 am
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
Posted: Wed Apr 11, 2007 5:46 am
by aaronhall
You can also change the patter delimiter -- '@' is a common replacement
Posted: Wed Apr 11, 2007 12:43 pm
by ChessclubFriend
feyd | Please use 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
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]
Posted: Wed Apr 11, 2007 12:47 pm
by feyd
the return value from preg_replace() isn't used, so PHP is doing work that you're choosing to throw away.

Posted: Wed Apr 11, 2007 4:58 pm
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 ?

Posted: Wed Apr 11, 2007 5:22 pm
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?
Posted: Wed Apr 11, 2007 5:27 pm
by Burrito
better yet, why not just do $questionNumber + $quesitonNumber1 in your pattern?