nedarta/yii2-clean-html-behavior

A Yii2 behavior to clean and sanitize HTML content in ActiveRecord attributes.

1.0.1 2025-02-19 11:47 UTC

This package is auto-updated.

Last update: 2025-04-19 12:34:04 UTC


README

License Packagist Version

A Yii2 behavior to clean and sanitize HTML content in ActiveRecord attributes.

Installation

Install the extension via Composer:

composer require nedarta/yii2-clean-html-behavior

Usage

Add the behavior to your ActiveRecord model:

use nedarta\behaviors\CleanHtmlBehavior;

public function behaviors()
{
    return [
        [
            'class' => CleanHtmlBehavior::class,
            'attributes' => ['content', 'description'],
            'htmlPurifierConfig' => [
                'HTML.Allowed' => 'p,b,i,u,ul,ol,li',
            ],
            'keepEmoji' => true, // Set to true to preserve emoji characters
        ],
    ];
}

Configuration

  • attributes: List of attributes to clean.

  • htmlPurifierConfig: Configuration for HtmlPurifier.

  • keepEmoji: Whether to preserve emoji characters.

  • preserveLineBreaks: Whether to preserve line breaks.

License

MIT