Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 23rd, 2012  |  syntax: None  |  size: 1.37 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env drush
  2. <?php
  3. // http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers
  4. // To run: ./img-clean.php @my-drush-alias
  5.  
  6. // Finds the first occurance of a <tag> </tag>
  7. // and replaces it with an empty string
  8. function replace_tag( $tag, $html, $self_closing = TRUE ) {
  9.   if($self_closing) $regex = '{<'.$tag.'(.*?)/>}';
  10.   else $regex = '{<'.$tag.'[^>]*>(.*?)</'.$tag.'>}';
  11.  
  12.   $tag = preg_quote($tag);
  13.   $clean_str = preg_replace($tag,
  14.                             '', // Replacement str
  15.                             $html,
  16.                             1); // Only replaces first occurance
  17.   return $clean_str;
  18. }
  19.  
  20. // Finds the first occurance of an <img />
  21. // tag and replaces it with an empty string
  22.  
  23. // Gets the nids that we want to cleanup
  24. $query = db_select('node', 'n');
  25. $query->condition('n.type', 'article')
  26.       ->fields('n', array('nid'));
  27. $result = $query->execute()->fetchAll(PDO::FETCH_ASSOC);
  28.  
  29. foreach($result as $nid) {
  30.   $node = node_load($nid['nid']);
  31.   $tag = 'a';
  32.   $node->body['und'][0]['value']      = replace_tag($tag,$node->body['und'][0]['value'],FALSE);
  33.   $node->body['und'][0]['value']      = replace_tag($node->body['und'][0]['value']);
  34.   $node->body['und'][0]['safe_value'] = replace_tag($tag,$node->body['und'][0]['safe_value'],FALSE);
  35.   $node->body['und'][0]['safe_value'] = replace_tag($node->body['und'][0]['safe_value']);
  36.   node_save($node);
  37. }