Some simple (but useful) examples

Last modified: December 2, 2009 - 10:11

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;

More examples will be added soon.

 
 

Drupal is a registered trademark of Dries Buytaert.