PHP code example of olegatro / html-sanitizer-relative

1. Go to this page and download the library: Download olegatro/html-sanitizer-relative 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/ */

    

olegatro / html-sanitizer-relative example snippets




use HtmlSanitizer\Extension\Basic\BasicExtension;
use HtmlSanitizer\SanitizerBuilder;

$builder = new SanitizerBuilder();
$builder->registerExtension(new BasicExtension());

//relative-a and relative-image
$builder->registerExtension(new \HtmlSanitizer\Extension\Relative\A\AExtension());
$builder->registerExtension(new \HtmlSanitizer\Extension\Relative\Image\ImageExtension());

$config = [
    'extensions' => ['basic', 'relative-a', 'relative-image'],
    'tags'       => [
        'div' => [
            'allowed_attributes' => ['class', 'title']
        ],
        'img' => [
            'allowed_attributes' => ['src', 'alt', 'title', 'class']
        ]
    ]
];

$sanitizer = $builder->build($config);

//Examples

$rawHtml = '<p><a href="https://github.com">github.com</a></p>';
$rawHtml .= '<p><a href="/github.com">github.com</a></p>';
$safeHtml = $sanitizer->sanitize($rawHtml);

//<p><a href="https://github.com">github.com</a></p>
//<p><a href="/github.com">github.com</a></p>



$rawHtml = '<p><img src="https://github.com/favicon.ico" alt="" class="favicon-class"></p>';
$rawHtml .= '<p><img src="favicon.ico" alt="github favicon"></p>';
$safeHtml = $sanitizer->sanitize($rawHtml);

//<p><img src="https://github.com/favicon.ico" alt class="favicon-class" /></p>
//<p><img src="favicon.ico" alt="github favicon" /></p>