?
Solved

Can php update a div tag directly?

Posted on 2008-11-07
12
Medium Priority
?
1,069 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:Rassac
12 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22904542
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
 
LVL 6

Expert Comment

by:fourice
ID: 22904581
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
 

Author Comment

by:Rassac
ID: 22906422
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Expert Comment

by:hielo
ID: 22907073
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
 

Author Comment

by:Rassac
ID: 22909937
I got this error:

Warning: preg_replace() [function.preg-replace]: Unknown modifier '[' in c:\Inetpub\wwwroot\savediv.php on line 10.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22910996
try:
$newContent = preg_replace('#<div id="replace">[^<]*</div>#', '<div id="replace">'.$appendContent."</div>", $oldContent);
0
 

Author Comment

by:Rassac
ID: 22911899
Thanks that worked great! :) May I ask you what the "  >[^<]*  " symbols mean please?
0
 

Author Comment

by:Rassac
ID: 22912249
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
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22914991
>>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
 

Author Comment

by:Rassac
ID: 22915920
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
 
LVL 82

Assisted Solution

by:hielo
hielo earned 2000 total points
ID: 22919140
>>(.+)
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
 

Author Closing Comment

by:Rassac
ID: 31514346
Thanks hielo!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question