You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
46 lines
1.3 KiB
<?php
|
|
function extractMeta($directory) {
|
|
$metaData = [];
|
|
|
|
if (!is_dir($directory)) {
|
|
throw new Exception("Directory not found: $directory");
|
|
}
|
|
|
|
foreach (new DirectoryIterator($directory) as $file) {
|
|
if ($file->isFile() && (
|
|
$file->getExtension() === 'md' ||
|
|
$file->getExtension() === 'php'
|
|
)) {
|
|
$contents = file_get_contents($file->getPathname());
|
|
|
|
// Extract title and image from the front matter
|
|
if (preg_match('/^-{3,}\s*(.*?)\s*-{3,}/ms', $contents, $matches)) {
|
|
$frontMatter = $matches[1];
|
|
|
|
// Extract title
|
|
preg_match('/title:\s*(.*?)\s*$/m', $frontMatter, $titleMatch);
|
|
$title = trim($titleMatch[1] ?? '');
|
|
|
|
// Extract image
|
|
preg_match('/image:\s*(.*?)\s*$/m', $frontMatter, $imageMatch);
|
|
$image = trim($imageMatch[1] ?? '');
|
|
|
|
$metaData[$file->getFilename()] = [
|
|
'title' => $title,
|
|
'image' => $image,
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
return $metaData;
|
|
}
|
|
|
|
// Test the function
|
|
try {
|
|
$directory = __DIR__ ;
|
|
$meta = extractMeta($directory);
|
|
print_r($meta);
|
|
} catch (Exception $e) {
|
|
echo "Error: " . $e->getMessage();
|
|
}
|
|
|