Skip to content

Instantly share code, notes, and snippets.

@ecorson
Created April 3, 2012 14:32
Show Gist options
  • Save ecorson/2292454 to your computer and use it in GitHub Desktop.
Save ecorson/2292454 to your computer and use it in GitHub Desktop.
Search and replace text using Views Bulk Operations
<?php
/**
* Search and replace using Views Bulk Operations
*/
$node = node_load($entity->nid); //load node form entity supplied by VBO
$haystack = $node->body['und'][0]['value']; //set node body to be haystack
$search = "Office of International Students and Scholars"; //Set the string to search for
$replace = "International Student & Scholar Services"; //String to replace target
$pos = strpos($string, $search);
//test if the string exists before attempting to replace
if ($pos != false) {
drupal_set_message(l($node->title, 'node/'.$entity->nid)); //link to node with change
$node->body['und'][0]['value'] = str_replace($search, $replace, $haystack);
node_save($node);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment