Forum Moderators: coopster
[hk.php.net...]
Then maybe str_replace to replace something with the new data you want? You`ll need something in your HTML code to make it easier. Maybe something like:
%CODE%
and then:
$file = readfile('file.html');
$file = str_replace("%CODE%", NEW_DATA_HERE, $file);
echo $file;
dc
Let's review.
- you have a page and it has an Add button.
- you want to use the Add button to add something to the page. What exactly? Text from a <textarea>?
- then you want to redisplay the page after hitting the Add button?
Imagine a PHP page that reads the data in an HTML page. While reading, the PHP script stops at a determined point (identified in the HTML code by some tag, like <stop>) and stores that section to a string $beginning. Then, it reads the rest of the page and stores it to a string $ending. The PHP page then rewrites the page, inserting another string between the two:
fwrite ($beginning.$middle.$ending); \\not sure if this is correct format, but just giving you the idea
My question is how to get the PHP code to stop in the middle of the HTML code, store it, and read and store the end of the code.
Let's assume that
$contents == contents of the HTML file that you have read into a variable.
$tag = tag you are using in your HTML like <!-- INSERT HERE -->
$pattern = '/(.*)' . $tag . '(.*)/';
$replace = '$1' . $middle . '$2';
$new_contents = preg_replace($pattern, $replace, $contents);
Then output $new_contents.
Something along those lines anyway. Depending on factors (is the tag unique, etc etc) it might be necessary to tweak the regex.
More on regular expressions here ... PCRE [php.net]
$filename = {html filename and path};
$html_original = file_get_contents($filename);
list($beginning, $ending) = explode("<stop>",$html_original);
$fp = fopen($filename, "w"); // open file
fwrite($fp, $beginning . $appended_text . $ending);
fclose($fp);