如何不使用Composer安装html-to-markdown库
Got it, let's walk through how to set up html-to-markdown without relying on Composer—since you're used to simpler setups like Parsedown, this should feel familiar once you know the steps:
Step 1: Grab the Source Code
First, head to the library's official repository (look for the release section) and download the latest stable source code as a ZIP file. Unzip this file into your project directory—for example, you might create a folder like html-to-markdown at your project root, and extract all the contents there.
Step 2: Set Up Autoloading
Unlike Parsedown which is a single file, html-to-markdown uses the PSR-4 namespace standard, so you'll need a simple autoloader to load its classes. Add this snippet to your project's main PHP file (or a dedicated autoload file):
spl_autoload_register(function ($class) { // Adjust this path to match where you extracted the library $base_dir = __DIR__ . '/html-to-markdown/src/'; $prefix = 'League\\HTMLToMarkdown\\'; // Skip classes not in this namespace if (strpos($class, $prefix) !== 0) { return; } // Convert namespace to file path $relative_class = substr($class, strlen($prefix)); $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; // Load the file if it exists if (file_exists($file)) { require_once $file; } });
Step 3: Use the Library
Once the autoloader is set up, you can use the library just like you would with Composer:
use League\HTMLToMarkdown\HtmlConverter; // Initialize the converter $converter = new HtmlConverter(); // Convert your HTML to Markdown $html = '<article><h2>My Sample Post</h2><p>This is a <strong>test</strong> paragraph.</p></article>'; $markdown = $converter->convert($html); echo $markdown;
Important Checks
- PHP Extensions: html-to-markdown requires the
domandxmlextensions, which are enabled by default in most PHP installations. If you run into errors, double-check that these extensions are active in yourphp.ini. - Stable Versions: Stick to tagged release versions (not the main development branch) to avoid unexpected bugs. When you need to update, just re-download the latest stable ZIP and replace the old files.
内容的提问来源于stack exchange,提问作者Robert

