[SOLVED] writing to an html file?
Moderator: General Moderators
-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
writing to an html file?
Hi all
Is it possible to write (using fwrite();) to an html file, in order to replace the content of an html tag by some other content?
Here's my goal:
I have some html includes pages that start with some comments (<!-- comment -->), and I use the info in the comment tag to sort the html files.
I'd like to be able to dynamically write new comments in these comment tags.
I have managed to "read" them, and return them individually, but can I re-write them?...
Is this possible?
Could you help me? (If you do please include code examples, I'm new to php)
Thanks a lot
Is it possible to write (using fwrite();) to an html file, in order to replace the content of an html tag by some other content?
Here's my goal:
I have some html includes pages that start with some comments (<!-- comment -->), and I use the info in the comment tag to sort the html files.
I'd like to be able to dynamically write new comments in these comment tags.
I have managed to "read" them, and return them individually, but can I re-write them?...
Is this possible?
Could you help me? (If you do please include code examples, I'm new to php)
Thanks a lot
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
heres the basics of it
1. [php_man]file_get_content[/php_man]
2. assign vars into array
ex. $vars = array('var1' => 'Var_Content');
3. foreach ($vars as $var)
--> replace the tag with content using [php_man]str_replace[/php_man], [php_man]preg_match[/php_man] and [php_man]preg_replace[/php_man]
4. echo the content
If your wanting more information I wrote a pretty simple templating system, contact me on msn I'll help you out.
1. [php_man]file_get_content[/php_man]
2. assign vars into array
ex. $vars = array('var1' => 'Var_Content');
3. foreach ($vars as $var)
--> replace the tag with content using [php_man]str_replace[/php_man], [php_man]preg_match[/php_man] and [php_man]preg_replace[/php_man]
4. echo the content
If your wanting more information I wrote a pretty simple templating system, contact me on msn I'll help you out.
-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
twigletmac | Help us, help you. Please use
basically I want to read and write and replace the comments.
I have managed to read them like this:
But I can't figure out how to write-replace in them...
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
thanks for your answer.
I don't use msn (just regular email).
I'd really appreciate your help.
Here are some details:
The pages I want to change (read and write to) are in a sub-directory called 'pages'.
Each page code starts with:Code: Select all
<!-- 1 --><title>t</title><!-- 4neighborhood --><!-- 5price --><!-- 6size -->I have managed to read them like this:
Code: Select all
$fp = fopen ("pages/$page", 'rb');
while ($line = fgetcsv ($fp, 200, "<")) {//this reads incrementally and uses the opening tag sign to separate them
if ($line[4] == "!-- 4blabla -->") {
echo "<p><a href="$pagedir/$page">" . strstr($line[2], '>') . "</a></p>";
break;
}
}
fclose ($fp);- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
twigletmac | Help us, help you. Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
thanks for the feedback.
But I don't really inderstand what I'm doing and I'm trying to put bits of code together hoping that it will work...
Just so you can help me, what I'm trying to do is:
read the page "latestLoft.php", find the string "4neighborhood" in it, and replace it with whatever the selected value of option menu in the form.
When I submit the form, nothing happens, not even an error. Please Help!...
Here's my code so far:Code: Select all
<?php
ini_set ('display_errors',1);
error_reporting (E_ALL & ~E_NOTICE);
if (isset ($_POST['submit'])) {
if ( !empty ($_POST['neighborhood'])) {
if ($fp = fopen ("pages/latestLoft.php", 'r+b')) {
$search = $fp ('4neighborhood');
$replace = $fp ($_POST['neighborhood']);
$result = str_replace ($search, $replace, $fp);
fclose ($fp);
print "<p> OK, DONE! <a href="f.php">[ go back ]</a></p>";
} else {
print "<p> it didn't work </p>";
}
} else {
print "<p>Select the Loft's neighborhood:</p>";
}
}
?>
<p>
<form name="form1" method="post" action="neighborhood.php">
<select name="neighborhood">
<option selected="selected" value="Select...">Select...</option>
<option value="Soho">Soho</option>
<option value="Upper West Side">Upper West Side</option>
<option value="Chelsea">Chelsea</option>
<option value="West Village">West Village</option>
<option value="Upper East Side">Upper East Side</option>
</select>
<input type="submit" name="Submit" value="Submit">
</form>
</p>-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
sorry I realized I didn't put my code between the appropriate tags...
so here's my code again:
and here's the form:
so here's my code again:
Code: Select all
<?php
ini_set ('display_errors',1);
error_reporting (E_ALL & ~E_NOTICE);
if (isset ($_POST['Submit'])) {
if ( !empty ($_POST['neighborhood'])) {
if ($fp = fopen ("pages/latestLoft.php", 'r+b')) {
$search = $fp ('4neighborhood');
$replace = $fp ($_POST['neighborhood']);
$result = str_replace ($search, $replace, $fp);
fclose ($fp);
print "<p> OK, DONE! <a href="f.php">[ go back ]</a></p>";
} else {
print "<p> it didn't work </p>";
}
} else {
print "<p>Select the Loft's neighborhood:</p>";
}
}
?>Code: Select all
<p>
<form name="form1" method="post" action="neighborhood.php">
<select name="neighborhood">
<option selected="selected" value="Select...">Select...</option>
<option value="Soho">Soho</option>
<option value="Upper West Side">Upper West Side</option>
<option value="Chelsea">Chelsea</option>
<option value="West Village">West Village</option>
<option value="Upper East Side">Upper East Side</option>
</select>
<input type="submit" name="Submit" value="Submit">
</form>
</p>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Change:
to
and see if you get any notices (error reporting really should be at it's highest possible level for development).
Mac
Code: Select all
error_reporting (E_ALL & ~E_NOTICE);Code: Select all
error_reporting (E_ALL);Mac
-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
I solved my problem, finaly.
here's the code:
here's the code:
Code: Select all
<?php
$original = file_get_contents ("pages/$page"); //this opens the page and returns its content as one string
$search = 'something';//this looks for the word 'something' in the page
$replace = 'something else'];//this says what it should be replaced with
$new_content = str_replace ($search, $replace, $original);//this creates the new string with the replacement
if ($fp = fopen ("pages/$page", 'w+b')) { //this opens the page again, in write mode
fwrite ($fp, $new_content); //this writes the new content
fclose ($fp);//closes the page
}
?>