convertImages($content); $content = $this->convertBold($content); $content = $this->convertItalic($content); $content = $this->convertUnderline($content); $content = $this->convertStrikethrough($content); $content = $this->convertCode($content); $content = $this->convertLinks($content); $content = $this->convertLists($content); $content = $this->convertHeaders($content); $content = $this->convertLineBreaks($content); $content = $this->cleanUp($content); return trim($content); } private function convertImages(string $content): string { $content = preg_replace('/]+src=["\']([^"\']+)["\'][^>]*>/i', '($1)', $content); return $content; } private function convertBold(string $content): string { $content = preg_replace('/]*>(.*?)<\/strong>/is', '**$1**', $content); $content = preg_replace('/]*>(.*?)<\/b>/is', '**$1**', $content); return $content; } private function convertItalic(string $content): string { $content = preg_replace('/]*>(.*?)<\/em>/is', '*$1*', $content); $content = preg_replace('/]*>(.*?)<\/i>/is', '*$1*', $content); return $content; } private function convertUnderline(string $content): string { $content = preg_replace('/]*>(.*?)<\/u>/is', '__$1__', $content); return $content; } private function convertStrikethrough(string $content): string { $content = preg_replace('/]*>(.*?)<\/s>/is', '~~$1~~', $content); $content = preg_replace('/]*>(.*?)<\/strike>/is', '~~$1~~', $content); $content = preg_replace('/]*>(.*?)<\/del>/is', '~~$1~~', $content); return $content; } private function convertCode(string $content): string { $content = preg_replace('/]*>(.*?)<\/code>/is', '`$1`', $content); $content = preg_replace('/]*>(.*?)<\/pre>/is', "```\n$1\n```", $content); return $content; } private function convertLinks(string $content): string { $content = preg_replace('/]+href=["\']([^"\']+)["\'][^>]*>(.*?)<\/a>/is', '[$2]($1)', $content); return $content; } private function convertLists(string $content): string { $content = preg_replace('/]*>(.*?)<\/li>/is', "\n• $1", $content); $content = preg_replace('/]*>/is', '', $content); $content = preg_replace('/<\/ul>/is', '', $content); return $content; } private function convertHeaders(string $content): string { $content = preg_replace('/]*>(.*?)<\/h1>/is', "\n\n## $1\n", $content); $content = preg_replace('/]*>(.*?)<\/h2>/is', "\n\n### $1\n", $content); $content = preg_replace('/]*>(.*?)<\/h3>/is', "\n\n#### $1\n", $content); $content = preg_replace('/]*>(.*?)<\/h4>/is', "\n\n##### $1\n", $content); $content = preg_replace('/]*>(.*?)<\/h5>/is', "\n\n###### $1\n", $content); $content = preg_replace('/]*>(.*?)<\/h6>/is', "\n\n###### $1\n", $content); return $content; } private function convertLineBreaks(string $content): string { $content = preg_replace('//i', "\n", $content); return $content; } private function cleanUp(string $content): string { $content = preg_replace('/]*>(.*?)<\/p>/is', "\n$1\n", $content); $content = preg_replace('/]*>(.*?)<\/div>/is', "\n$1\n", $content); $content = preg_replace('/]*>(.*?)<\/span>/is', '$1', $content); $content = strip_tags($content); $content = preg_replace('/ /', ' ', $content); $content = preg_replace('/&/', '&', $content); $content = preg_replace('/</', '<', $content); $content = preg_replace('/>/', '>', $content); $content = preg_replace('/"/', '"', $content); $content = preg_replace('/\n{3,}/', "\n\n", $content); return $content; } public function extractImages(string $html): array { preg_match_all('/]+src=["\']([^"\']+)["\'][^>]*>/i', $html, $matches); return $matches[1] ?? []; } public function removeImages(string $html): string { return preg_replace('/]+>/i', '', $html); } }