Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,500 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,732 people online right now. Registration is fast and FREE... Join Now!




Write to an HTML file using PHP

 
Reply to this topicStart new topic

Write to an HTML file using PHP

jnkrois
5 Mar, 2008 - 04:27 PM
Post #1

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 3

Hello. Thanks for being out here to help PHP newbies like me.
I need some advice.
I want my script to write into an HTML file using the "fwrite" function. The problem I have is that the script writes to the HTML file but at the end of the file, outside the appropriate tags.
What I want to do is to make the script with a string (just text) to a specific tag inside the body tag in HTML.
Here is what I have so far.
CODE

<?php
$my_file = "some_file.php";
$fh = fopen($my_file, 'a') or die("can't open file");
$string_data = "Lorem ipsum";
fwrite($fh, $string_data);
echo "File succesfully written";
?>

This code works even though is simple and given my lack of experience, but this is the result i get:
CODE

<html >
<head>
</head>
<body>
<div id="my_div">
<!-- Content goes here-->
</div>
</body>
</html>
[b]Lorem ipsum[/b]


Again, how can I specify to what div the script writes the string?

Thanks a lot in advance for any advice that I can get in this matter.
Juan Carlos

This post has been edited by no2pencil: 5 Mar, 2008 - 07:50 PM
User is offlineProfile CardPM
+Quote Post

ericode
RE: Write To An HTML File Using PHP
5 Mar, 2008 - 07:38 PM
Post #2

D.I.C Head
Group Icon

Joined: 9 Dec, 2006
Posts: 89


Dream Kudos: 75
My Contributions
It may be that I don't fully understand your problem. If that is the case, this probably won't help you, but I will continue. To me it seems like you are using PHP, but not using it in the easy way you should. Simply make your script as so:

CODE

<html >
<head>
</head>
<body>
<div id="my_div">

<?php

    echo "Lorem ipsum";

?>

</div>
</body>
</html>


Then when you run that script, it will print out the HTML like you want and execute the php code within to produce:

CODE

<html >
<head>
</head>
<body>
<div id="my_div">
Lorem ipsum
</div>
</body>
</html>



If you do want your script to actually be writing other files, you'll have to be moving the file pointer around (reading and writing) when you work on the file. See the fopen manual here.

This post has been edited by ericode: 5 Mar, 2008 - 07:40 PM
User is offlineProfile CardPM
+Quote Post

jnkrois
RE: Write To An HTML File Using PHP
5 Mar, 2008 - 10:56 PM
Post #3

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 3

Thanks for your kind reply.

I believe that indeed I did not explain my self correctly or as you said I'm not using PHP correctly.
Let me try again:
CODE


The following code allows me to write into an existing html file.

<?php
$my_file = "my_file.php";
$fh = fopen($my_file, 'a') or die("can't open file");
$string_data = "any_random_text";
fwrite($fh, $string_data);
echo "File successfully written";
?>

The problem with this script is that it would write the "$string_data" (any_random_text) at the end of the html file, after the </html> closing tag.

What I would like to do is to add a function (or whatever it needs) to modify the code (I believe this part of the script: "fwrite($fh, $string_data);") so it locates a specific tag (lets say "my_div") in the html file and add the "$string_data" to it; like so:

<body>
<div id="my_div">
any_given_text
</div>
</body>
...


Thanks again and sorry for the trouble.
Juan Carlos

This post has been edited by jnkrois: 5 Mar, 2008 - 10:57 PM
User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: Write To An HTML File Using PHP
6 Mar, 2008 - 05:11 AM
Post #4

D.I.C Regular
Group Icon

Joined: 20 Feb, 2003
Posts: 270

template parser basics is what you are doing.

you are using and apend write.

and file is not in a sring.
put php tag in html for the string.

CODE

<html >
<head>
</head>
<body>
<div id="my_div">
<?=$string_data?>
</div>
</body>
</html>


<?
$my_file = "some_file.php";
$f_content = file_get_contents($my_file);
$string_data = "Lorem ipsum";
file_put_content($fh, $string_data);
?>




This post has been edited by SpaceMan: 6 Mar, 2008 - 05:21 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 08:09PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month