Is it possible to programmaticaly change the HTML rendered by Drupal 7? Imagine that we have a project completely finished, with lots of modules, themes, etc. Is it possible at all to have a module, which will be called at the last stage, and change the HTML output. For example, we want to make a ereg_replace of a img tag, and we don't want to deep into the modules. Is there a simple hook which can accomplish this extremely simple task ?
If you want to alter the rendered content of a page, you start by adding a post_render function to the node:

/**
 * Implements hook_node_view_alter().
 */
function MODULE_node_view_alter(&amp;$build) {
  $build['#post_render'][] = 'MODULE_wrap_images';
}


And then in that post render function, you can do what you need to do. In the example below, I am replacing float:left and float:right with a wrapper div:

function MODULE_wrap_images($content) {
  // Find all links within the content and extract the complete &lt;img&gt; tag.
  if (preg_match_all('#&lt;img.*?/&gt;#i', $content, $matches)) {

    // Loop matches and process.
    foreach ($matches[0] as $index =&gt; $match) {

      // Align images left.
      if (stripos($match, 'float: left;')) {
        $new = '&lt;div class="content-img content-img-left"&gt;' . str_ireplace('float: left;', '', $match) . '&lt;/div&gt;';
        $content = str_ireplace($match, $new, $content);
      }

      // Align images right.
      if (stripos($match, 'float: right;')) {
        $new = '&lt;div class="content-img content-img-right"&gt;' . str_ireplace('float: right;', '', $match) . '&lt;/div&gt;';
        $content = str_ireplace($match, $new, $content);
      }
    }
  }

  return $content;
}