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(); }