Issue renaming PHP files to HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
geronimo
Forum Newbie
Posts: 5
Joined: Thu May 14, 2009 9:59 am

Issue renaming PHP files to HTML

Post 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!
Last edited by Benjamin on Thu May 14, 2009 12:21 pm, edited 1 time in total.
Reason: Changed code type from text to php.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Issue renaming PHP files to HTML

Post 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...
geronimo
Forum Newbie
Posts: 5
Joined: Thu May 14, 2009 9:59 am

Re: Issue renaming PHP files to HTML

Post 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!
Last edited by Benjamin on Thu May 14, 2009 12:21 pm, edited 1 time in total.
Reason: Changed code type from text to php.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Issue renaming PHP files to HTML

Post 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!
geronimo
Forum Newbie
Posts: 5
Joined: Thu May 14, 2009 9:59 am

Re: Issue renaming PHP files to HTML

Post 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!
Last edited by Benjamin on Thu May 14, 2009 12:21 pm, edited 1 time in total.
Reason: Changed code type from text to php.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Issue renaming PHP files to HTML

Post 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.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Issue renaming PHP files to HTML

Post 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);
 
}
?>
 
geronimo
Forum Newbie
Posts: 5
Joined: Thu May 14, 2009 9:59 am

Re: Issue renaming PHP files to HTML

Post by geronimo »

Beautiful!! It works. Thank you!!
Post Reply