Can php update a div tag directly?

Hi,

Is it possible to fopen() an html file, update / write in an html file with PHP? I would like to update / write in specific div tags. I know it' s possible to write in a given file but am not sure if this can be done within div tags as well.

I know a workaround, where PHP writes in an htmlFileA.html and uses innerHTML in htmlFileB.html to display htmlFileA.html within it. Is it possible not to use  htmlFileA.html and update directly in  a given div tag found in htmlFileB.html ?

Thanks.
RassacAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
>>May I ask you what the "  >[^<]*  " symbols mean please?
I used a regular expression. It basically says:

Look for <div id="replace">
followed by anything that is NOT a "less than" character which may appear/exist zero or more times
followed by </div>
 
>>also, the problem I found is that of the div tag has another tag inside
right. Again, that's because it is looking for any thing that is NOT a <. Since any tag starts with <, that will be a problem. I thought your div would have only "plain text", not XML/HTML.
 
try:
 
$newContent = preg_replace('#<div id="replace">.*?(</div>)#', '<div id="replace">'.$appendContent."</div>", $oldContent);

Open in new window

0
 
Ray PaseurCommented:
Yes, it's possible to do this.  However it might be easier to keep track of your work if you established each segment of the HTML in its own file and let PHP operate on them independently.  Then when you persent the web page, reassemble the HTML from the component files via a server-side include or via PHP.
0
 
fouriceCommented:
Something like this:
<?php
$appendContent = "This needs to be in a div";
 
$filename = "./file.txt";
$handle = fopen($filename, "r");
$oldContent = fread($handle, filesize($filename));
fclose($handle);
 
$newContent = str_replace("<div id=\"replace\"></div>", "<div id=\"replace\">".$appendContent."</div>", $oldContent);
 
$writeHandle = fopen($filename, "w+");
fwrite($writeHandle, $newContent);
fclose($writeHandle);
?>

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
RassacAuthor Commented:
Hi,

I have slightly changed the code, put html file instead of txt and inserted 2 echos. The problem is that the $newContent contains the same data as the $oldContent.

here is my code below:
<?php
$appendContent = "This needs to be in a div";
 
$filename = "testFile.html";
$handle = fopen($filename, "r");
$oldContent = fread($handle, filesize($filename));
fclose($handle);
echo $oldContent;
 
$newContent = str_replace("<div id=\"replace\"></div>", "<div id=\"replace\">".$appendContent."</div>", $oldContent);
echo $newContent ;
$writeHandle = fopen($filename, "w+");
fwrite($writeHandle, $newContent);
fclose($writeHandle);
?>
 
[testFile.html]
<html>
<body>
<div id="replace">helloooooooo</div>
</body>
</html>

Open in new window

0
 
hieloCommented:
try:
<?php
$appendContent = "This needs to be in a div";
 
$filename = "testFile.html";
$handle = fopen($filename, "r");
$oldContent = fread($handle, filesize($filename));
fclose($handle);
echo $oldContent;
 
$newContent = preg_replace('<div id="replace">[^<]*</div>', '<div id="replace">'.$appendContent."</div>", $oldContent);
echo $newContent ;
$writeHandle = fopen($filename, "w+");
fwrite($writeHandle, $newContent);
fclose($writeHandle);
?>

Open in new window

0
 
RassacAuthor Commented:
I got this error:

Warning: preg_replace() [function.preg-replace]: Unknown modifier '[' in c:\Inetpub\wwwroot\savediv.php on line 10.
0
 
hieloCommented:
try:
$newContent = preg_replace('#<div id="replace">[^<]*</div>#', '<div id="replace">'.$appendContent."</div>", $oldContent);
0
 
RassacAuthor Commented:
Thanks that worked great! :) May I ask you what the "  >[^<]*  " symbols mean please?
0
 
RassacAuthor Commented:
also, the problem I found is that of the div tag has another tag inside, lets say

<div id="replace"><strong>HELLO</strong></div>

the html file is not updated.

The code updates data in the div function only if there is no other tag embedded inside.
0
 
RassacAuthor Commented:
Thanks that worked fine. I found another possible solution. Do you think there are any major differences between your solution and the one below?

I will still grant you your points since you have already answered my original question. :)
$newContent = preg_replace('#<div id="replace">(.+)</div>#', '<div id="replace">'.$appendContent."</div>", $oldContent);

Open in new window

0
 
hieloConnect With a Mentor Commented:
>>(.+)
The + requires that there be at least one "something" in between the div. So if you have:
<div id="replace"></div> it will not insert the new value. You need to account for the possibility that the div is empty. So you need to use the * instead.
0
 
RassacAuthor Commented:
Thanks hielo!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.