4

Some background info: I'm Building a D7 module that also sends out a message to users of the site. I want the site maintainers to be able to define the message by using 'tokens' like this:

$string = "hello :username, there is a new node called :nodenodename_with_a_link on the :website"; // defined in the GUI

I could use str_replace to replace those 'tokens' like so:

$find = array(':username', ':nodenodename_with_a_link', ':website');
$replace = array($username, $node, $sitename); // generated form the db
$newstring = str_replace($find, $replace, $string);

But I'm wondering if this is also the correct way to go in Drupal (7)?

2

You can either create your own tokens in a custom module or use strtr (it's easier for multiple search and replace operations than str_replace).

To see how to create custom tokens and integrate with token module see token_example module in Examples.

  • I knew it there had to be a 'cleaner' drupal way thanks for pointing me to the token_example! – FLY Oct 3 '12 at 8:17
  • hmm just now I realize I could use the t() function for this... ? – FLY Oct 3 '12 at 8:40
  • You're welcome - glad I helped :) – Aram Boyajyan Oct 3 '12 at 8:40
  • Yes, you could use that one as well. – Aram Boyajyan Oct 3 '12 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.