Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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. :)
Comment | File | Size | Author |
---|---|---|---|
#1 | strip_attributes.txt | 3.4 KB | WillHall |
strip_attributes.txt | 3.39 KB | WillHall |
Comments
Comment #1
WillHall CreditAttribution: WillHall commentedTypo in help text.
Comment #2
gstout CreditAttribution: gstout commentedI 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.
Comment #3
gstout CreditAttribution: gstout commentedI 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
Comment #4
twistor CreditAttribution: twistor commented