Some simple (but useful) examples

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

This page is a collection of simple replacement rules you can use for yourself. Before you can use any of this, you should have a filter as a container.

Simple replace (case sensitive)

This will convert all occurrence of foo into bar, but it is case-sensitive, means that it won't work for Foo, or fOO.

  • Pattern: /foo/
  • PHP Code: off
  • Replacement: bar

Simple replace (case insensitive)

This will convert all occurrence of foo into bar, and it is not case-sensitive, means that it will work for Foo, and fOO.

  • Pattern: /foo/i
  • PHP Code: off
  • Replacement: bar

Tag replacer

This will convert all occurrence of <hello> into Hello World. It is not case sensitive, and it can handle spaces within tag, like < hello >, by using \s* pattern. That pattern means "zero, or more spaces here".

  • Pattern: /<\s*hello\s*>/i
  • PHP Code: off
  • Replacement: <strong>Hello World!</strong>

Adds link to drupal.org on the occurrence of <drupal/>. Notice that there is no \s* pattern, so it is not intended to replace < drupal />. You should enter exactly <drupal/>. And notice that backslash (\/), because slash (/) has a special meaning in regular expressions, so we must escape it using backslash.

  • Pattern: /<drupal\/>/i
  • PHP Code: off
  • Replacement: <a href="http://drupal.org">drupal.org</a>

Add some text at the top of content body

This will add BEGIN: at the top of your content body.

  • Pattern: /^/
  • PHP Code: off
  • Replacement: <strong>BEGIN:</strong><br/>

Add some text below the content body

This will add :END below your content body.

  • Pattern: /$/
  • PHP Code: off
  • Replacement: <br/><strong>:END</strong>

Anchorize subtitles

Precedes all <h2> and <h3> tags (subtitles) with an anchor named with the sanitized title. This allows to link to a specific section of a page. An example: http://drupal.org/node/210555#anchorize-subtitles
Use your browser inspector to reveal the anchors.

  • Pattern: /<h(2|3)>(.*)<\/h(2|3)>/i
  • PHP Code: on
  • Replacement:
    $name = trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($matches[2])), '-');
    $result = '<p><a name="' . $name . '"></a></p>' . $matches[0];

Auto numbering mechanism

Each <an/> will be converted into sequential number 1, 2, 3, 4, ... This replacement rule is an example of numbering mechanism.

  • Pattern: /<\s*an\s*\/>/i
  • PHP Code: on
  • Replacement:
    static $num;
    
    $num++;
    
    return $num;
    

Correct paths in img-tags

This will correct all absolute and relative paths in <img /> tags in your content body. It helps if you installed drupal in a sub-directory of your webroot and insert the paths as /sites/default/files.

  • Pattern: /(<img[^>]+(src="([\w|\.|\/|\-|\_]+)")[^>]+\/>)/
  • PHP Code: on
  • Replacement: $result = str_replace($matches[3], url($matches[3]), $matches[1]);

This will allow you to create links inside your site with automatic name. For example, you write [url==23]description for editing[/url] and you get a link like <a href="path_to_your_node">title of my node 23</a>.

  • Pattern: /\[url==([\d]+)](.+?)\[\/url]/
  • PHP Code: on
  • Replacement:
    $node = node_load($matches[1]);
    if (!$node) {
        $result = '<span class="custom_filter_warning">-Node not found, check the node id you entered-</span>';
        }
    else {
        $result = l($node->title, 'node/' . $matches[1]);
        }
    return $result;

When an internal link would lead to access denied or not found, then the link is stripped only leaving the description text.

  • Pattern: /<a\shref=\"([^\"]*)\">(.*)<\/a>/siU
  • PHP Code: on
  • Replacement:
    $link=preg_replace("/^\//","",$matches[1]);
    $result=drupal_valid_path($link)?$matches[0]:$matches[2];

Using with geshi

This is a complex example, it show how to use custom filter and the geshi library to highlight source code in pre tags.
Download the geshi library from http://qbnz.com/highlighter/ , unpack it and copy to sites/all/libraries/geshi

  • Pattern: /<pre([.\s\S]*?)>([.\s\S]*?)<\/pre>/i
  • PHP Code: on
  • Replacement:
    require_once DRUPAL_ROOT . "/sites/all/libraries/geshi/geshi.php";
    
    $attr = array();
    $start = true;
    $pieces = explode("=", $matches[1]);
      
    foreach($pieces as $piece)  {
      if ($start) {
        $key = $piece;
        $start = false;
      } else {
        $p = explode('"', $piece);
          $attr[trim($key)] = $p[1];
          $key = trim($p[2]);
      }
    }
    
    
    $geshi = new GeSHi(decode_entities($matches[2]), $attr['language']);
    $geshi->set_overall_style('font-size: 1.4em;');
    $geshi->set_header_type(GESHI_HEADER_PRE_TABLE);
    
    if ($attr['lines']) {
        $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
        if ($attr['lines'] > 1) {
             $geshi->start_line_numbers_at($attr['lines']);
        }
    }
    $result = '<div class="geshi_code">'  . $geshi->parse_code() . '</div>';
    
    return $result;

More examples will be added soon.

Help improve this page

Page status: Not set

You can: