PHP code example of stevebauman / purify
1. Go to this page and download the library: Download stevebauman/purify 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/ */
stevebauman / purify example snippets
use Stevebauman\Purify\Facades\Purify;
$input = '<script>alert("Harmful Script");</script> <p style="border:1px solid black" class="text-gray-700">Test</p>';
// Returns '<p>Test</p>'
$cleaned = Purify::clean($input);
use Stevebauman\Purify\Facades\Purify;
$array = [
'<script>alert("Harmful Script");</script> <p style="border:1px solid black" class="text-gray-700">Test</p>',
'<script>alert("Harmful Script");</script> <p style="border:1px solid black" class="text-gray-700">Test</p>',
];
$cleaned = Purify::clean($array);
// array [
// '<p>Test</p>',
// '<p>Test</p>',
// ]
var_dump($cleaned);
use Stevebauman\Purify\Facades\Purify;
$config = ['HTML.Allowed' => 'div,b,a[href]'];
$cleaned = Purify::config($config)->clean($input);
// config/purify.php
'configs' => [
// ...
'comments' => [
// Some configuration ...
],
]
use Stevebauman\Purify\Facades\Purify;
$cleanedContent = Purify::config('comments')->clean(request('content'));
// config/purify.php
'serializer' => null,
use Stevebauman\Purify\Casts\PurifyHtmlOnGet;
class Post extends Model
{
protected $casts = [
'content' => PurifyHtmlOnGet::class,
];
}
use Stevebauman\Purify\Facades\Purify;
class Post extends Model
{
public function getContentAttribute($value)
{
return Purify::clean($value);
}
}
// config/purify.php
'configs' => [
// ...
'other' => [
// Some configuration ...
],
]
protected $casts = [
'content' => PurifyHtmlOnGet::class.':other',
];
namespace App;
use HTMLPurifier_HTMLDefinition;
use Stevebauman\Purify\Definitions\Definition;
class CustomDefinition implements Definition
{
/**
* Apply rules to the HTML Purifier definition.
*
* @param HTMLPurifier_HTMLDefinition $definition
*
* @return void
*/
public static function apply(HTMLPurifier_HTMLDefinition $definition)
{
// Customize the HTML purifier definition.
}
}
// config/purify.php
'definitions' => \App\CustomDefinitions::class,
use Stevebauman\Purify\Definitions\Html5Definition;
class CustomDefinition implements Definition
{
public static function apply(HTMLPurifier_HTMLDefinition $definition)
{
Html5Definition::apply($definition);
// ...
}
}
namespace App;
use HTMLPurifier_HTMLDefinition;
use Stevebauman\Purify\Definitions\Definition;
class TrixPurifierDefinitions implements Definition
{
/**
* Apply rules to the HTML Purifier definition.
*
* @param HTMLPurifier_HTMLDefinition $definition
*
* @return void
*/
public static function apply(HTMLPurifier_HTMLDefinition $definition)
{
$definition->addElement('figure', 'Inline', 'Inline', 'Common');
$definition->addAttribute('figure', 'class', 'Class');
$definition->addAttribute('figure', 'data-trix-attachment', 'Text');
$definition->addAttribute('figure', 'data-trix-attributes', 'Text');
$definition->addElement('figcaption', 'Inline', 'Inline', 'Common');
$definition->addAttribute('figcaption', 'class', 'Class');
$definition->addAttribute('figcaption', 'data-trix-placeholder', 'Text');
$definition->addAttribute('a', 'rel', 'Text');
$definition->addAttribute('a', 'tabindex', 'Text');
$definition->addAttribute('a', 'contenteditable', 'Enum#true,false');
$definition->addAttribute('a', 'data-trix-attachment', 'Text');
$definition->addAttribute('a', 'data-trix-content-type', 'Text');
$definition->addAttribute('a', 'data-trix-id', 'Number');
$definition->addElement('span', 'Block', 'Flow', 'Common');
$definition->addAttribute('span', 'data-trix-cursor-target', 'Enum#right,left');
$definition->addAttribute('span', 'data-trix-serialize', 'Enum#true,false');
$definition->addAttribute('img', 'data-trix-mutable', 'Enum#true,false');
$definition->addAttribute('img', 'data-trix-store-key', 'Text');
}
}
namespace App;
use HTMLPurifier_CSSDefinition;
use Stevebauman\Purify\Definitions\CssDefinition;
class CustomCssDefinition implements CssDefinition
{
/**
* Apply rules to the CSS Purifier definition.
*
* @param HTMLPurifier_CSSDefinition $definition
*
* @return void
*/
public static function apply(HTMLPurifier_CSSDefinition $definition)
{
// Customize the CSS purifier definition.
$definition->info['text-align'] = new \HTMLPurifier_AttrDef_Enum(
['right', 'left', 'center', 'start', 'end'],
false,
);
}
}
// config/purify.php
'css-definitions' => \App\CustomCssDefinition::class,
'serializer' => storage_path('app/purify'),
bash
php artisan vendor:publish --provider="Stevebauman\Purify\PurifyServiceProvider"
shell
php artisan purify:clear
bash
php artisan vendor:publish --provider="Stevebauman\Purify\PurifyServiceProvider"