
Untitled
By: a guest on
Aug 23rd, 2012 | syntax:
None | size: 1.37 KB | hits: 7 | expires: Never
#!/usr/bin/env drush
<?php
// http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers
// To run: ./img-clean.php @my-drush-alias
// Finds the first occurance of a <tag> </tag>
// and replaces it with an empty string
function replace_tag( $tag, $html, $self_closing = TRUE ) {
if($self_closing) $regex = '{<'.$tag.'(.*?)/>}';
else $regex = '{<'.$tag.'[^>]*>(.*?)</'.$tag.'>}';
$tag = preg_quote($tag);
$clean_str = preg_replace($tag,
'', // Replacement str
$html,
1); // Only replaces first occurance
return $clean_str;
}
// Finds the first occurance of an <img />
// tag and replaces it with an empty string
// Gets the nids that we want to cleanup
$query = db_select('node', 'n');
$query->condition('n.type', 'article')
->fields('n', array('nid'));
$result = $query->execute()->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $nid) {
$node = node_load($nid['nid']);
$tag = 'a';
$node->body['und'][0]['value'] = replace_tag($tag,$node->body['und'][0]['value'],FALSE);
$node->body['und'][0]['value'] = replace_tag($node->body['und'][0]['value']);
$node->body['und'][0]['safe_value'] = replace_tag($tag,$node->body['und'][0]['safe_value'],FALSE);
$node->body['und'][0]['safe_value'] = replace_tag($node->body['und'][0]['safe_value']);
node_save($node);
}