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>

Links

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

"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;

Remove inaccessible links

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.

Comments

Boobaa’s picture

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;
    
damontgomery’s picture

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.

wirka’s picture

Maybe will be better if you add return value of base_path() before calls function drupal_get_path_alias(). This will works if drupal installation in subdirectory.

The pattern will be:
/node\/[0-9]*/i

Replacement Text (PHP):

$result = base_path() . drupal_get_path_alias($matches[0]);
return $result;

Thanks

ledom’s picture

Each <img> will be converted into <a title="" href=""><img></a>

  • Pattern: #(<img.*?src="(.*?)".*?title="(.*?)"[^>]*>)#i
  • PHP Code: off
  • Replacement: <a title="$3" rel="lightbox2" href="$2">$1</a>
ugintl’s picture

Nice module. How can I convert @username to mentions with user profile link and #hashtag into links site wide. Just like twitter. I am specifically interested to use them in status updates (statuses). There are mentions and hashtag modules, but they have some limitations. Mentions module requires [@username] this bracket and hashtags do not work on statuses.

I am not a developer. Any help is appreciated.