Community Documentation

Some simple (but useful) examples

Last updated January 3, 2011. Created by arhip on January 16, 2008.
Edited by yukare, NoRandom, stBorchert, ibex. Log in to edit this page.

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 occurence 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 occurence 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 occurence 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>

Links

Adds link to drupal.org on the occurence 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>

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]);

"Intelligent" links

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;

Using with geshi

This is a complex example, it show how to use custom filter and the geshi library to hightlight 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.

Comments

Convert #nid to link

This will allow you to create node links inside your site with automatic name. For example, you write #23 and you get a link like <a href="path_to_your_node_23">#23: title of your node 23</a>, or leave it as a simple #23 if node #23 does not exist.

  1. Pattern: /#([\d]+)/
  2. PHP Code: on
  3. Replacement:
    $node = node_load($matches[1]);
    $result = '#'. $matches[1];
    if ($node) {
      $result = l($result .': '. $node->title, 'node/' . $matches[1]);
    }
    return $result;

Add a legend to a image

Hello I would like to add a legend to an image from the alt-value, for example:

Only local images are allowed.

would be converted to

Only local images are allowed.

My image

If the alt-Value is empty the legend would not be created. Can this be done with custom filter or do I look in the wrong direction?

Thanx

Filter to replace node urls with aliases

Pattern:

/node\/[0-9]*/i

Replacement Text (PHP):
$result = drupal_get_path_alias($matches[0]);
return $result;

What this does:
This allows people to enter content such as

<a href="/node/123">Link Here</a>

and have the link output with the alias:

<a href="/my-lovely-page">Link Here</a>

If the alias does not exist, it does not replace the text.

Note: Use this at the end of your filters list.

About this page

Drupal version
Drupal 5.x, Drupal 6.x, Drupal 7.x

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.