Search code examples
phphtmlhtmlpurifier

Adding attributes to a tag using htmlpurifier


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.


Solution

  • 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)