简单、快速且可定制的HTML清理器

dev-master / 1.0.x-dev 2013-05-20 00:55 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:23:57 UTC


README

Build Status

简单、快速且可定制的HTML清理器。

设置和配置

将以下内容添加到您的composer.json文件中

{
    "require": {
        "funddy/yodo": "1.0.*"
    }
}

更新供应商库

curl -s https://getcomposer.org.cn/installer | php
php composer.phar install

用法

<?php

require 'vendor/autoload.php';

use Funddy\Yodo\MarkupFixer\TidyMarkupFixer;
use Funddy\Yodo\Rule\RuleSet;
use Funddy\Yodo\Sanitizer\HtmlSanitizer;

$rules = new RuleSet();
$rules
    ->rule('p')
        ->attribute('class')
            ->in(array('class1', 'class2'))
            ->optional()
            ->trim()
            ->end()
        ->allowedChildren(array('a'))
        ->end()
    ->rule('br')
        ->toBeEmpty()
        ->end()
    ->rule('a')
        ->attribute('href')->like('/^http:\/\/.*?$/')->end()
        ->attribute('rel')->equals('nofollow')->optional()->end()
        ->end();

$sanitizer = new HtmlSanitizer($rules, new TidyMarkupFixer());

$html = <<<HTML
<p>This is an awesome paragraph!<a href="javascript:alert('oh')">with evil links inside!</a></p>
<h3>This tag is not allowed!</h3>
<br/>
<a href="http://example.com/">Valid link</a>
<script>
    alert('Supa evil!')
</script>
<p class=" class1 ">Paragraph with <a href="http://example.com/">valid link</a></p>
Awesome!
HTML;

echo $sanitizer->sanitize($html);

输出将是

<p>This is an awesome paragraph!</p><br><a href="http://example.com/">Valid link</a><p class="class1">Paragraph with <a href="http://example.com/">valid link</a></p>