How Can I Convert PHP Files to Static HTML Using PowerShell?

0
8
Asked By CuriousCoder123 On

I'm a total newbie looking to convert over 2000 dynamic PHP files into static HTML files for archiving my website. The PHP files are pretty simple, mostly containing five lines of PHP for Title and Description, along with an include for a header file that sets up the page structure. I also need to consider a small number of scripts in my '/scripts' folder. Before running the PowerShell script I've created, I'd like some feedback to ensure it's functioning correctly. Here's my script:

```powershell
# --- Configuration ---
$sourceDir = "C:pathtoyourphp_files"
$targetDir = "C:pathtoyourhtml_output"
$cssDir = "C:pathtoyourphp_filescss"
$imgDir = "C:pathtoyourphp_filesimages"

if (!(Test-Path $targetDir)) { New-Item -ItemType Directory -Path $targetDir }

# --- Helper Function for Robust Extraction ---
function Get-PhpVar {
param($pattern, $content, $default)
# This regex looks for: $variable_name = "value"; OR $array['key'] = "value";
# It accounts for optional spaces and different quote types.
if ($content -match $pattern) { return $Matches[1].Trim() }
return $default
}

$phpFiles = Get-ChildItem -Path $sourceDir -Filter "*.php"

foreach ($file in $phpFiles) {
Write-Host "Converting: $($file.Name)" -ForegroundColor Cyan
$content = Get-Content -Path $file.FullName -Raw

# Flexible Extraction Patterns
$titlePattern = '(?i)$(?:page)?title(?:[[''"]title[''"]])?s*=s*[''"](.+?)[''"]s*;'
$descPattern = '(?i)$(?:meta)?description(?:[[''"]description[''"]])?s*=s*[''"](.+?)[''"]s*;'
$canonPattern = '(?i)$(?:page)?canonical(?:[[''"]canonical[''"]])?s*=s*[''"](.+?)[''"]s*;'

$title = Get-PhpVar $titlePattern $content "Default Title"
$desc = Get-PhpVar $descPattern $content ""
$canon = Get-PhpVar $canonPattern $content $file.Name.Replace(".php", ".html")

# Process PHP Includes
$content = [regex]::Replace($content, '(?i)(?:include|require)(?:_once)?s*(?s*[''"](.+?.php)[''"]s*)?s*;', {
param($m)
$includePath = Join-Path $sourceDir $m.Groups[1].Value
if (Test-Path $includePath) { return Get-Content $includePath -Raw }
return ""
})

# Clean remaining PHP and assemble HTML5
$cleanBody = $content -replace '(?s)<?php.*??>', ''

$htmlOutput = @"

$title

$cleanBody

"@

# Link Replacement (.php -> .html)
$htmlOutput = $htmlOutput -replace 'hrefs*=s*["'']([^"'']+.)php(["'']?)', 'href="$1html$2'

# Save
$newName = $file.BaseName + ".html"
$htmlOutput | Out-File -FilePath (Join-Path $targetDir $newName) -Encoding utf8
}

# --- Asset Migration ---
Write-Host "Syncing Assets..." -ForegroundColor Green
@($cssDir, $imgDir) | ForEach-Object {
if (Test-Path $_) {
$folderName = Split-Path $_ -Leaf
Copy-Item -Path $_ -Destination (Join-Path $targetDir $folderName) -Recurse -Force
}
}

Write-Host "Success! Your site is now static HTML5." -ForegroundColor Goldenrod
```

3 Answers

Answered By TechExplorer On

Have you thought about accessing your PHP files directly through your web server? If that's an option, you could use something like Invoke-WebRequest to fetch the generated HTML output directly from the server, which might save you some hassle compared to parsing PHP files yourself.

Answered By WebDevNinja On

If you're just learning PowerShell, using AI might actually complicate things for you instead of helping. For tasks like this, consider using Wget or a similar web crawler instead; they'll do the job without the learning curve that PowerShell might throw your way.

CuriousCoder123 -

I specifically requested a script that runs on Windows desktop systems, and PowerShell was recommended. I appreciate the input, but right now PowerShell seems the most direct option according to the guidance I got.

Answered By DevGuru42 On

First off, you won't get any data from databases in this setup. If your PHP files rely on a database, you'll need to dump that data and generate the HTML from it. Otherwise, you're just converting the PHP files themselves, which won't work since they rely on dynamic sources. You might want to modify your approach and have your PHP script create each static page based on the database values instead of trying to convert PHP to HTML directly. But if your PHP files don't access any databases, this should generally work as is—just remember to update the paths in the script accordingly!

HelpfulPerson99 -

The AI-generated instructions were quite clear, and it even provided options for database access—though it sounds like that's not relevant for you. If you want to experiment, I'd suggest running a test with the provided script to see how it processes a single file!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.