Page 1 of 1

Issue renaming PHP files to HTML

Posted: Thu May 14, 2009 10:27 am
by geronimo
Okay, I'm another newbie here and first off want to apologize for my basic ignorance. Please be kind.

What I'm trying to do is run a script that opens my PHP files and then saves them in HTML. This is what I'm using:

Code: Select all

<?php
$files = file("filelist.txt");
for($i = 0; $i < count($files); $i++) {
ob_start();
include($files[$i]);
$page = ob_get_contents();
ob_end_clean();
if(strpos($files[$i], ".php") > -1) {
$file = fopen(substr($files[$i], 0,strlen($files[$i])-4) . ".html",'w');
fputs($file, $page);
fclose($file);
}
}
?>
The Problem:
The problem I'm having is with trimming the URLs in the text file (filelist.txt). Right now I'm just testing it with 2 URL strings like this:

Filelist.txt
/var/www/vhosts/mysite.com/httpdocs/firstpage.php
/var/www/vhosts/mysite.com/httpdocs/secondpage.php


After running the script, this is what I get:

firstpage..html
secondpage.html


The second page is fine, but obviously the first is not with the additional . (dot) before the extension. Now I'm pretty sure the issue has to do with the extra character after each line until the last one in the text file, which is somewhat of a mystery to me. I've tried playing around with the trim() function but to no avail. For example, I took line 5 from the script above:

include($files[$i]);

and replaced it with:

include (trim($files[$i]));

But that didn't solve my problem.

If anyone has an idea on what I'm doing wrong and how to fix it, I'd really appreciate it. If there's a simple way to amend the script with the trim function or if I can do something to the text file, either is fine. I am just hoping for a solution.

Thank you in advance!

Re: Issue renaming PHP files to HTML

Posted: Thu May 14, 2009 10:32 am
by crazycoders
You were almost there, the trim() just must be applied to the fopen too because i don't see anyother potential errors in your code...

Re: Issue renaming PHP files to HTML

Posted: Thu May 14, 2009 11:08 am
by geronimo
I tried this:

Code: Select all

<?php
$files = file("filelist.txt");
for($i = 0; $i < count($files); $i++) {
ob_start();
include (trim($files[$i]));
$page = ob_get_contents();
ob_end_clean();
if(strpos($files[$i], ".php") > -1) {
$file = fopen(trim(substr($files[$i], 0,strlen($files[$i])-4)) . ".html",'w');
fputs($file, $page);
fclose($file);
}
}
?php>
But unfortunately it didn't work. I'm sure I'm not applying trim to fopen correctly. Any ideas?

Thanks!

Re: Issue renaming PHP files to HTML

Posted: Thu May 14, 2009 12:01 pm
by crazycoders
Ok try this...i use this to trim the extension, but you'll have to change a little bit your code or it will look really weird:

1.Reverse the filename
2.Explode in two parts and list back in $ext, $filename
3.Reverse filename again and append the extension

SO:

Code: Select all

 
$phpfilename = trim($files[$i]);
list($ext, $targetfilename) = explode('.', strrev($phpfilename), 2);
$targetfilename = strrev($targetfilename).'.html';
 
This should work without problem!

Re: Issue renaming PHP files to HTML

Posted: Thu May 14, 2009 12:18 pm
by geronimo
I'm feeling like a dork here, but could you please show me how the entire script should look now? I tried this, but it didn't work:

Code: Select all

<?php
$files = file("filelist.txt");
for($i = 0; $i < count($files); $i++) {
ob_start();
include (trim($files[$i]));
$page = ob_get_contents();
ob_end_clean();
if(strpos($files[$i], ".php") > -1) {
$phpfilename = trim($files[$i]);
list($ext, $targetfilename) = explode('.', strrev($phpfilename));
$targetfilename = strrev($targetfilename).'.html';
fputs($targetfilename, $page);
fclose($targetfilename);
}
}
?>
I really am a newbie.

Thanks again!

Re: Issue renaming PHP files to HTML

Posted: Thu May 14, 2009 12:36 pm
by crazycoders
You got it right except that it needs a explode('.', $targetfilename, 2);... see the 2 at the end, i had to edit my post cause i forgot it. It forces the filename to split in 2 parts only which is important in this case because you may have 2 periods in a filename sometimes.

Re: Issue renaming PHP files to HTML

Posted: Thu May 14, 2009 12:38 pm
by crazycoders
Humm, where is your fopen?

Code: Select all

 
<?php
$files = file("filelist.txt");
for($i = 0; $i < count($files); $i++) {
 
$phpfilename = trim($files[$i]);
list($ext, $targetfilename) = explode('.', strrev($phpfilename), 2);
$targetfilename = strrev($targetfilename).'.html';
 
ob_start();
include (trim($files[$i]));
$page = ob_get_contents();
ob_end_clean();
 
$fp = fopen($targetfilename, 'w');
fputs($fp, $page);
fclose($fp);
 
}
?>
 

Re: Issue renaming PHP files to HTML

Posted: Thu May 14, 2009 1:03 pm
by geronimo
Beautiful!! It works. Thank you!!