Forum Moderators: coopster

Message Too Old, No Replies

Appending Data to HTML

         

inveni0

10:34 pm on Dec 10, 2005 (gmt 0)

10+ Year Member



I'm looking to create a PHP Page that will have a button labeled "ADD" that will do the following:

1) Read an HTML document and store as a string.
2) Append a text document to the center of the HTML document.

How do I tell the PHP page where to insert the text data?

dreamcatcher

11:22 am on Dec 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use the readfile function to read the file:

[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

inveni0

2:33 pm on Dec 11, 2005 (gmt 0)

10+ Year Member



That's what I'm curious about. How do I tell PHP where to insert the data? What HTML tags would I use?

ergophobe

3:27 pm on Dec 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure I understand. You would not use HTML tags, you would use PHP code along the lines that Dreamcatcher has suggested.

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?

inveni0

3:50 pm on Dec 11, 2005 (gmt 0)

10+ Year Member



I'm not trying to change the PHP page, I'm trying to change an external HTML page.

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.

ergophobe

6:41 pm on Dec 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ahhh! Got it! You probably need a regex.

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.

inveni0

3:16 am on Dec 13, 2005 (gmt 0)

10+ Year Member



I'm not sure I understand these two lines:

$pattern = '/(.*)' . $tag . '(.*)/';
$replace = '$1' . $middle . '$2';

What is the /(.*) and its reciprocal? Where do $1 and $2 get their values? Which line assigns the HTML tag to the $tag variable?

Could you explain this in a bit more depth?

inveni0

3:43 pm on Dec 14, 2005 (gmt 0)

10+ Year Member



Any word on how this works?

coopster

6:41 pm on Dec 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It is regular expression search and replace. The parenthesis are surrounding anything before your $tag and capturing that in a variable, $1. The next set of capturing parenthesis does the same, except now it moves on to $2, etc.

More on regular expressions here ... PCRE [php.net]

Mr_Fern

10:05 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



I saw you mention an arbitrary code such as <stop>. That would be useful. You could have the php script do an explode on this particular code, and then append the data. Example Code:

$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);