3

I am trying to use HTMLPurifier to transform HTML by adding class attributes to paragraphs.

For example, for this input HTML:

<p>This is a paragraph</p>
<p>Another one</p>

This would be the output:

<p class="myclass">This is a paragraph</p>
<p class="myclass">Another one</p>

I read the doc and some forum post on that site but i could not figure it out how exactly I should do it?

Thanks in advance.

1 Answer 1

3

Here is a quick and dirty example that you can test on your own:

<?php

require_once 'lib/library/HTMLPurifier.auto.php';

class HTMLPurifier_AttrTransform_AnchorClass extends HTMLPurifier_AttrTransform
{
    public function transform($attr, $config, $context)
    {
      // keep predefined class
      if (isset($attr['class']))
      {
        $attr['class'] .= ' myclass';
      }
      else
      {
        $attr['class'] = 'myclass';
      }

      return $attr;
    }
}

$dirty_html = '<a href=""></a>
<a target="_blank" href=""></a>
<a href="" class="toto"></a>
<a href="" style="oops"></a>';

$config     = HTMLPurifier_Config::createDefault();
$htmlDef    = $config->getHTMLDefinition(true);

$anchor     = $htmlDef->addBlankElement('a');
$anchor->attr_transform_post[] = new HTMLPurifier_AttrTransform_AnchorClass();

$purifier   = new HTMLPurifier($config);

$clean_html = $purifier->purify($dirty_html);

var_dump($clean_html);
It outputs:

string '<a href="" class="myclass"></a>

<a href="" class="myclass"></a>

<a href="" class="toto myclass"></a>

<a href="" class="myclass"></a>' (length=135)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.