PHP code example of masroore / html2text

1. Go to this page and download the library: Download masroore/html2text library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

masroore / html2text example snippets


use Kaiju\Html2Text\Html2Text;

$converter = new Html2Text();
echo $converter->convert($html);

# assign a pre-processing callback function. (transform href links)
$converter->setPreProcessingCallback(fn (string $s) => preg_replace('%<\s*a[^>]*href=[\'"](.*?)[\'"][^>]*>([\s\S]*?)<\/\s*a\s*>%i', '$2 ($1)', $s));

# assign a tag-replacement callback function. (replace <li> tags)
$converter->setTagReplacementCallback(fn (string $s) => preg_replace('/<\s*li[^>]*>/i', "\n- ", $s));

# post-processing hook
$converter->setPostProcessingCallback(...);

# process HTML
echo $converter->convert($html);