PDA

For continued disscussion on this topic : changing file content



jewishj
06-06-2001, 12:15 AM
In this one....

I'm trying to go through a directory of .htm files and go in the source and change the content inbetween...

<a name="top">

and

<!--START CONTENT-->

I am running IIS 5.0 through win2kpro, nothing happens when I try to run this script...

here's what I have so far...




<?php

if ($dir = @opendir("C:\webpage")) {
while($file = readdir($dir)) {
if(substr_count($file,".htm") > 0){

$fd = fopen ($file, "a+");
$contents = fread ($fd, filesize ($file));
$startpoint=explode("<a name="top" />",$filename);
$endpoint=explode("<!--START CONTENT //-->",$filename);
$finalsrc="$startpoint[0]\n<!--#include file=\"includes/header.htm\"-->$endpoint[1]";
fwrite($fd,$finalsrc);
fclose($fd);

}
}
closedir($dir);
}

?>


this doesn't do anything, all the permissions are set to "full control", which includes write, read, delete, modify, etc.

I'm stuck, can someone please help me with my PHP writers block

thanx in advance

jonsteele
06-06-2001, 12:25 AM
Hi,

why do you have a server side include in the middle of the php? does this file have a .shtml or .php extension? :)

You'll have to read the contents of that file too, and put that variable in this line:

$finalsrc="$startpoint[0]\n$var\n$endpoint[1]";

Jon.

jewishj
06-06-2001, 01:45 AM
What I have is a directory full of .htm files. Within each I want to replace the header, which includes the sidebar, and replace it with the include tag.... so I am not trying to include a file into the php script, I am trying to write the tag into the source of all of the html files. And I am also trying to rename all the html files as $file.shtml

I have another post asking for help with that....

http://www.webxpertz.net/forums/showthread.php?t=7536

thanx

jonsteele
06-06-2001, 03:40 AM
Sorry, my mistake :).

First: shouldn't you have $contents in your explode functions, not $filename?

Secondly: I think you need the strpos() function. http://www.php.net/manual/en/function.strpos.php

This'll return the first occurence of a substring in a string.

If you still have problems I'll be happy to help.

Jon.

jewishj
06-06-2001, 10:27 AM
ok well, it now works somewhat, but to work the kinks out of it I modified the script (for now) to work on only one file.... here's what I have...



<?php


$filename="advertise.htm";
$fd = fopen ($filename, "a+");
$contents = fread ($fd, filesize ($filename));
fclose($fd);
$startpt=strpos($contents,"<a name=\"top\">");
$startpoint=substr($contents,0,$startpt);
$endpt=strpos($contents,"<!--START CONTENT //-->");
$midpt=strpos($contents,"<!--END CONTENT//-->");
$footer=substr($contents,($midpt+20));
$endpoint=substr($contents,($endpt+23),$midpt);
$finalsrc="$startpoint\n<!--#include file=\"includes/header.htm\"-->$endpoint\n<!--#include file=\"includes/footer.htm\"-->$footer";
echo "<xmp>$contents</xmp><br><br><b>Will be changed to</b><br><br><xmp>$finalsrc</xmp><br><br>$startpt,$endpt,$midpt";
echo "<br><br><xmp>$endpoint</xmp><br><br><br><xmp>$footer</xmp>";


?>



for some reason $endpoint is to much, in other words lets say this is my page



<html>
<body>
<a name="top">

<!--START CONTENT //-->

this page not yet up

<!--END CONTENT//-->

</html>



what I get is the resulting...

$startpoint=


<html>
<body>



$endpoint=


this page not yet up

<!--END CONTENT//-->

</html>


$footer=


<!--END CONTENT//-->

</html>



the problem here is $endpoint as I said, it's including what should be $footer, but it's also including the part that I want to delete...

any help would be much appreciated as always....

one more question while im here...

how can I check for 2 different things while finding the strpos,

e.g.

change this javascript snippet to php, (the ex. still uses php vars)


if($contents.indexOf("<!--START CONTENT//-->")!=-1){
$endpnt="<!--START CONTENT//-->";
}
else if($contents.indexOf("<!--START CONTENT //-->")!=-1){
$endpt="<!--START CONTENT //-->";
}



thanx

btw like the avatar jon

jonsteele
06-07-2001, 11:28 PM
Hi,

for your question, you could use strpos() and .indexOf in the same way, as they both return numbers (instead of using string.indexOf, you'd use strpos($index...))



I think this line

$endpoint=substr($contents,($endpt+23),$midpt);
should be

$endpoint=substr($contents,($endpt+23),($midpt-$endpt));

I think in your code you're thinking that the third paramete of the substr is where the string should stop, where it's actually the length of the string it should return.

Jon.

jewishj
06-08-2001, 06:32 AM
ok now I get the following problem,

The header is replaced with the correct includes tag, the footer includes tag is in the right place, but after the footer includes tag, the footer still apears...

to get an example go to,

http://65.9.144.72/replacecontenttest2.php

to see the actual source and what it's being changed to, here's the code used again...



<?php


$filename="advertise.htm";
$fd = fopen ($filename, "a+");
$contents = fread ($fd, filesize ($filename));
fclose($fd);
$startpt=strpos($contents,"<a name=\"top\" />");
$startpoint=substr($contents,0,$startpt);
$endpt=strpos($contents,"<!--START CONTENT //-->");
$midpt=strpos($contents,"<!--END CONTENT//-->");
$footer=substr($contents,($midpt+20));
$endpoint=substr($contents,($endpt+23),($midpt-$endpt));
$finalsrc="$startpoint\n<!--#include file=\"includes/header.htm\"-->$endpoint\n<!--#include file=\"includes/footer.htm\"-->$footer";
echo "<xmp>$contents</xmp><br><br><b>Will be changed to</b><br><br><xmp>$finalsrc</xmp><br><br>$startpt,$endpt,$midpt";
echo "<br><br><xmp>$endpoint</xmp><br><br><br><xmp>$footer</xmp>";



?>

jonsteele
06-08-2001, 11:29 PM
So you want the text between

<!-- START FOOTER//-->

and

<!--END FOOTER//-->

to be replaced...right?

If so, use this function I wrote. Just pass the filename, first delimiter, second delimiter, and text to insert. You can use it a couple of times to replace the header/footer:



<?
function insert_in($filename, $first_delimiter, $second_delimiter, $text_to_insert){

//$fd = fopen ($filename, "a+");
//$contents = fread ($fd, filesize ($filename));
//fclose($fd);

$content = $filename;
$firstpart = substr($content,0,(strlen($first_delimiter)+strpos($content,$first_delimiter)));

$lastpart = substr($content,strpos($content,$second_delimiter));

$final = $firstpart."\n".$text_to_insert."\n".$lastpart;
return $final;
}
?>


Here's an example:



//example usage:
<?
echo "<xmp>";
echo insert_in($thetext, "<!-- START FOOTER//-->", "<!--END FOOTER//-->", "<!--#include file='includes/footer.htm'-->");
echo "</xmp>";
?>


Jon :).

jewishj
06-09-2001, 03:45 PM
seems to work, thanx

so then I should just be able to add if statements to check whether the file contains... <!--START HEADER//--> or <!--START HEADER //--> and then go through the function based on that right?

oh, one more thing, Jon, (or anyone) do you have any ideas on what to do with the other question (posted a link in a previous post in this thread)...

jonsteele
06-09-2001, 04:05 PM
Yeah, something like this (also I forgot to uncomment the lines...I put it in because I was testing it using a textarea in a form):



<?
function insert_in($filename, $first_delimiter, $second_delimiter, $text_to_insert){

$fd = fopen ($filename, "a+");
$contents = fread ($fd, filesize ($filename));
fclose($fd);

$pos1 = strpos($content,$first_delimiter);
if ($pos1 === false){ exit(); }
$firstpart = substr($content,0,(strlen($first_delimiter)+$pos1));

$pos2 = strpos($content,$second_delimiter);
if ($pos2 === false){ exit(); }
$lastpart = substr($content,$pos2);

$final = $firstpart."\n".$text_to_insert."\n".$lastpart;
return $final;
}
?>


Jon.