How can PHP be used to read all HTML files in a folder and save specific text content to separate text files?
To read all HTML files in a folder and save specific text content to separate text files using PHP, you can use the glob() function to get a list of HTML files in the directory, then iterate through each file to extract the desired text content using regular expressions or a HTML parser library like DOMDocument. Once you have the text content, you can save it to separate text files using file_put_contents().
<?php
// Path to the directory containing HTML files
$htmlDir = '/path/to/html/files/';
// Get a list of HTML files in the directory
$htmlFiles = glob($htmlDir . '*.html');
foreach ($htmlFiles as $file) {
// Read the HTML file
$htmlContent = file_get_contents($file);
// Extract specific text content using regular expressions or HTML parser
$specificText = ''; // Extract specific text content here
// Save specific text content to separate text files
$textFile = $htmlDir . basename($file, '.html') . '.txt';
file_put_contents($textFile, $specificText);
}
?>