Certainly! Below is a PHP script that takes a URL as input, fetches the web page, extracts the title and meta description, and creates a Markdown (.md) file with this information. The script avoids extracting the full content of the article to respect copyright policies.
<?php
/**
* Script to create a Markdown file from a given URL.
* It fetches the page title and meta description and saves them into a .md file.
*/
// The URL to fetch (you can replace this with any other URL)
$url = "https://www.wsj.com/tech/cybersecurity/arion-kurtaj-hacker-468e6cad";
// Fetch the HTML content of the URL
$html = fetchUrlContent($url);
// Check if content was fetched successfully
if ($html === false) {
echo "Failed to fetch the URL content.";
exit;
}
// Parse the HTML and extract the title and meta description
list($title, $description) = extractMetaData($html);
// Create the Markdown content
$markdownContent = createMarkdownContent($title, $description, $url);
// Define the filename (sanitize the title to create a filename)
$filename = sanitizeFileName($title) . ".md";
// Save the Markdown content to a file
if (file_put_contents($filename, $markdownContent) !== false) {
echo "Markdown file '$filename' has been created successfully.";
} else {
echo "Failed to create the Markdown file.";
}
/**
* Fetches the content of a URL using cURL.
*
* @param string $url The URL to fetch.
* @return string|false The content of the URL or false on failure.
*/
function fetchUrlContent($url)
{
$ch = curl_init($url);
// Set cURL options
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => 'Mozilla/5.0 (PHP script)',
]);
$content = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
return false;
}
curl_close($ch);
return $content;
}
/**
* Extracts the title and meta description from HTML content.
*
* @param string $html The HTML content.
* @return array An array containing the title and meta description.
*/
function extractMetaData($html)
{
$doc = new DOMDocument();
// Suppress errors due to malformed HTML
@$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
// Extract the title
$titleNodes = $xpath->query('//title');
$title = $titleNodes->length > 0 ? $titleNodes->item(0)->nodeValue : 'No Title Found';
// Extract the meta description
$metaDesc = '';
$metaNodes = $xpath->query('//meta[@name="description"]');
if ($metaNodes->length > 0) {
$metaDesc = $metaNodes->item(0)->getAttribute('content');
} else {
$metaDesc = 'No Description Found';
}
return [$title, $metaDesc];
}
/**
* Creates the Markdown content.
*
* @param string $title The title of the page.
* @param string $description The meta description of the page.
* @param string $url The original URL.
* @return string The formatted Markdown content.
*/
function createMarkdownContent($title, $description, $url)
{
$markdown = "# " . $title . "\n\n";
$markdown .= $description . "\n\n";
$markdown .= "Original article: [" . $url . "](" . $url . ")\n";
return $markdown;
}
/**
* Sanitizes a string to be used as a filename.
*
* @param string $filename The original filename.
* @return string The sanitized filename.
*/
function sanitizeFileName($filename)
{
// Replace any non-word characters with an underscore
$sanitized = preg_replace('/[^\w\-]/', '_', $filename);
// Limit the filename length
return substr($sanitized, 0, 200);
}
?>
How the Script Works:
-
fetchUrlContent: Uses cURL to fetch the content of the provided URL. It follows any redirects and sets a user agent to mimic a regular browser.
-
extractMetaData: Parses the HTML content using DOMDocument and DOMXPath to extract the page <title> and the <meta name="description"> content.
-
createMarkdownContent: Formats the extracted information into Markdown syntax. It includes the title as an H1 header, the description as a paragraph, and the original URL as a link.
-
sanitizeFileName: Cleans up the title to create a safe filename by removing or replacing invalid characters.
-
Main Execution:
- Fetches the URL content.
- Extracts metadata.
- Creates Markdown content.
- Saves the content into a
.md file named after the page title.
Note: This script avoids scraping the full content of the article to respect copyright laws. It only uses publicly available metadata.
Usage Instructions:
-
Replace the URL:
- Change the
$url variable at the top of the script to the URL you want to process.
-
Run the Script:
- Execute the script in a PHP environment. Make sure you have PHP installed and the necessary permissions to create files.
-
Check the Output:
- After running, the script will create a Markdown file in the same directory with a name derived from the page title.
Example Output:
For the provided URL, the script might create a file named Arion Kurtaj, the Rockstar Hacker Who Hit Nvidia and Uber.md with the following content:
# Arion Kurtaj, the Rockstar Hacker Who Hit Nvidia and Uber
The 18-year-old member of the Lapsus$ group was one of two teens behind breaches at major companies.
Original article: [https://www.wsj.com/tech/cybersecurity/arion-kurtaj-hacker-468e6cad](https://www.wsj.com/tech/cybersecurity/arion-kurtaj-hacker-468e6cad)
Please ensure you comply with all relevant laws and terms of service when using this script.