Page 1 of 1

writing to an html file?

Posted: Tue Oct 12, 2004 8:27 pm
by sebnewyork
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

Posted: Tue Oct 12, 2004 8:47 pm
by John Cartwright
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.

Posted: Tue Oct 12, 2004 11:42 pm
by sebnewyork
twigletmac | Help us, help you. Please use

Code: Select all

and

Code: 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

&lt;!-- 1 --&gt;&lt;title&gt;t&lt;/title&gt;&lt;!-- 4neighborhood --&gt;&lt;!-- 5price --&gt;&lt;!-- 6size --&gt;
basically I want to read and write and replace the comments.

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);
But I can't figure out how to write-replace in them...

Posted: Wed Oct 13, 2004 3:55 am
by twigletmac
For replacements - [php_man]str_replace()[/php_man] or if they are more complicated that just replace this exact thing with this other thing then [php_man]preg_replace()[/php_man].

For writing back to the file - [php_man]implode()[/php_man] and [php_man]fwrite()[/php_man].

Mac

Posted: Wed Oct 13, 2004 11:53 am
by sebnewyork
twigletmac | Help us, help you. Please use

Code: Select all

and

Code: 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>

Posted: Wed Oct 13, 2004 2:10 pm
by sebnewyork
sorry I realized I didn't put my code between the appropriate tags...
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>";
   }
}                                       
?>
and here's the form:

Code: Select all

&lt;p&gt;
&lt;form name="form1" method="post" action="neighborhood.php"&gt;
  &lt;select name="neighborhood"&gt;
&lt;option selected="selected" value="Select..."&gt;Select...&lt;/option&gt;
    &lt;option value="Soho"&gt;Soho&lt;/option&gt;
    &lt;option value="Upper West Side"&gt;Upper West Side&lt;/option&gt;
    &lt;option value="Chelsea"&gt;Chelsea&lt;/option&gt;
    &lt;option value="West Village"&gt;West Village&lt;/option&gt;
    &lt;option value="Upper East Side"&gt;Upper East Side&lt;/option&gt;
  &lt;/select&gt;
&lt;input type="submit" name="Submit" value="Submit"&gt;
&lt;/form&gt;
&lt;/p&gt;

Posted: Thu Oct 14, 2004 4:00 am
by twigletmac
Change:

Code: Select all

error_reporting (E_ALL & ~E_NOTICE);
to

Code: Select all

error_reporting (E_ALL);
and see if you get any notices (error reporting really should be at it's highest possible level for development).

Mac

Posted: Thu Oct 14, 2004 4:46 pm
by sebnewyork
I solved my problem, finaly.

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
}
?>