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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
hieloCommented:
>>(.+)
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.