This plugin strips attributes from html tags and allows you to optionally append a new attribute string. Also ensures affected tags are converted to lowercase.

Drop into /sites/all/modules/feeds_tamper/plugins/strip_attributes.inc

<?php

$plugin = array(
  'form' => 'feeds_tamper_strip_attributes_form',
  'callback' => 'feeds_tamper_strip_attributes_callback',
  'validate' => 'feeds_tamper_strip_attributes_validate',
  'description' => 'feeds_tamper_strip_attributes_description',
  'machine_name' => 'feeds_tamper_strip_attributes_machine_name',
  'name' => 'Strip attributes',
  'multi' => 'loop',
  'category' => 'HTML',
);

function feeds_tamper_strip_attributes_form($importer, $element_key, $settings) {
  $form = array();
  $form['help']['#markup'] = t('HTML tag to remove attributes from:');
  $form['affected_tags'] = array(
    '#type' => 'textfield',
    '#title' => t('Affected tag'),
    '#default_value' => isset($settings['affected_tags']) ? $settings['affected_tags'] : '',
    '#description' => t('The affected tag, such as %a', array('%a' => '<a>', '%b' => '<em>')),
  );

  $form['appended_attributes'] = array(
    '#type' => 'textfield',
    '#title' => t('Appended attributes'),
    '#default_value' => isset($settings['appended_attributes']) ? $settings['appended_attributes'] : '',
    '#description' => t('You can append attributes here, such as %a %b.', array('%a' => 'style="text-align: center"', '%b' => 'title="Hooray"')),
  );  
  return $form;
}

function feeds_tamper_strip_attributes_description($settings) {
  $desc = 'Strip attributes from HTML tags.';
  if (!empty($settings['affected_tags'])) {
    $desc .= ' Affecting: <' . $settings['affected_tags'] . '> tags and Appending: ' . $settings['appended_attributes'];
  }
  return $desc;
}

function feeds_tamper_strip_attributes_machine_name($settings) {
  return 'strip_html_attributes';
}

function feeds_tamper_strip_attributes_validate(&$settings) {
  $settings['affected_tags'] = trim($settings['affected_tags']);
  $settings['appended_attributes'] = trim($settings['appended_attributes']);
}

function feeds_tamper_strip_attributes_callback($result, $item_key, $element_key, &$field, $settings) {
  $field = _feeds_tamper_strip_attributes($field, $settings);
}

function _feeds_tamper_strip_attributes(&$field, $settings) {
  $lengthfirst = 0;
  $lengthend = 0;
  
  while (strstr(substr($field, $lengthfirst), "<" . $settings['affected_tags']) != "") {
    $tag_start = $lengthfirst + strpos(substr($field, $lengthfirst), "<" . $settings['affected_tags']);

    $partafterwith = substr($field, $tag_start);

    $img = substr($partafterwith, 0, strpos($partafterwith, ">") + 1);
    $img = str_replace(" =", "=", $img);

    $out = "<" . strtolower($settings['affected_tags']);
    
    if (!empty($settings['appended_attributes'])) {
      $out .= " " . $settings['appended_attributes'];
    }

    $out .= ">";

    $partafter = substr($partafterwith, strpos($partafterwith, ">") + 1);
    $field = substr($field, 0, $tag_start) . $out . $partafter;
    $lengthfirst = $tag_start + 3;
  }

  while (strstr(substr($field, $lengthend), "</" . $settings['affected_tags']) != "") {
    $tag_start = $lengthend + strpos(substr($field, $lengthend), "</" . $settings['affected_tags']);

    $partafterwith = substr($field, $tag_start);

    $img = substr($partafterwith, 0, strpos($partafterwith, ">") + 1);
    $img = str_replace(" =", "=", $img);

    $out = "</" . strtolower($settings['affected_tags']) . ">";

    $partafter = substr($partafterwith, strpos($partafterwith, ">") + 1);
    $field = substr($field, 0, $tag_start) . $out . $partafter;
    $lengthfirst = $tag_start + 3;
  }
    
  return $field;
}

Hope its helps someone, enjoy. :)

CommentFileSizeAuthor
#1 strip_attributes.txt3.4 KBWillHall
strip_attributes.txt3.39 KBWillHall
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

WillHall’s picture

FileSize
3.4 KB

Typo in help text.

gstout’s picture

I tried to re-roll this for 6.x but I failed miserably I'm not sure what here is not compatible with d6. Could one of you fine sirs re-roll it or at least point me in the right direction.

Thanks so much.

gstout’s picture

I should mention that when I run this plug-in as is PHP times out. Something in this plug-in is either causing an error or and endless loop that won't resolve on d6

twistor’s picture

Component: Miscellaneous » Plugins
Status: Active » Needs work
Issue tags: +Needs tests