https://github.com/masroore/php-html2text
A PHP package to convert HTML into a plain text format
https://github.com/masroore/php-html2text
html html-parser html2text
Last synced: about 1 year ago
JSON representation
A PHP package to convert HTML into a plain text format
- Host: GitHub
- URL: https://github.com/masroore/php-html2text
- Owner: masroore
- License: mit
- Created: 2022-06-13T01:26:39.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-13T02:07:08.000Z (almost 4 years ago)
- Last Synced: 2025-02-28T23:50:44.564Z (about 1 year ago)
- Topics: html, html-parser, html2text
- Language: PHP
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# A PHP package to convert HTML into plain text -- no HTML tags allowed in the output.
[](https://packagist.org/packages/masroore/html2text)
[](https://github.com/masroore/php-html2text/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/masroore/php-html2text/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[](https://packagist.org/packages/masroore/html2text)
## Overview ##
**masroore/html2text** is a PHP package that converts a page of HTML into clean, easy-to-read plain ASCII text.
## Installation
> **Requires [PHP 8.0+](https://php.net/releases/)**
You can install the package via composer:
```bash
composer require masroore/html2text
```
## Usage
Extract text from HTML:
```php
use Kaiju\Html2Text\Html2Text;
$converter = new Html2Text();
echo $converter->convert($html);
```
**Callback functions**
You are able to change process of formatting by providing callbacks in pre-processing, tag-replacing and post-processing:
```php
# 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
$converter->setTagReplacementCallback(fn (string $s) => preg_replace('/<\s*li[^>]*>/i', "\n- ", $s));
# post-processing hook
$converter->setPostProcessingCallback(...);
# process HTML
echo $converter->convert($html);
```
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Thank you for considering to contribute to Html2Text. All the contribution guidelines are mentioned [here](CONTRIBUTING.md).
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Masroor Ehsan](https://github.com/masroore)
- [All Contributors](../../contributors)
## License
Html2Text is an open-sourced software licensed under the [MIT license](LICENSE.md).