Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to implement it with theme_preprocess function. Every my attempt fails.

function mdrupal_mono_preprocess_page(&$variables) {
  $variables['title'] = drupal_get_title($variables['node']);
  $title = $variables['title'];
  if($title) {
    $words = explode(' ', $title);
    $words[0] = '<span>' . $words[0] . '</span>';
    $title = implode(' ', $words);
share|improve this question
drupal_get_title doesn't accept parameters – xurshid29 Apr 4 '14 at 8:29
If I am sure, then you want to display html markup within title of the node. – Jayendra Kainthola Apr 4 '14 at 8:37
yes @JayendraKainthola – Daler Madaminov Apr 4 '14 at 8:43
@DalerMadaminov, you can use better approach than this. – Jayendra Kainthola Apr 4 '14 at 8:44
and what would that be? @JayendraKainthola – Daler Madaminov Apr 4 '14 at 8:45
up vote 1 down vote accepted

in my case this is working perfectly in bartik_preprocess_page() (bartik theme):

  $pageTitle = drupal_get_title();
  $pageTitleArr = explode(' ', $pageTitle);
  $firstPart = '<span>' . $pageTitleArr[0] . '</span>';
  $pageTitleArr[0] = $firstPart;
  $title = implode(' ', $pageTitleArr);

  drupal_set_title($title, PASS_THROUGH);
  $variables['title'] = $title;
share|improve this answer
you should also sanitize and strip html tags or unsafe characters (except span tag) in title variable.. – xurshid29 Apr 4 '14 at 13:05

Not really a Drupal question, but you're setting the wrong variable

$title = implode(' ', $words);

should be

$variables['title'] = implode(' ', $words);

Or, change

$title = $variables['title'];


$title = &$variables['title'];

instead. Either will work

share|improve this answer
yeah that was a mistake, but the code still doesn't work – Daler Madaminov Apr 4 '14 at 8:27

You can add some pseudotags like [span] or [i] in your node title and can use below function in your template.php file.

function bb2html($text) {
  $bbcode = array(
                  "[strong]", "[/strong]",
                  "[b]",  "[/b]",
                  "[u]",  "[/u]",
                  "[i]",  "[/i]",
                  "[em]", "[/em]"
  $htmlcode = array(
                "<strong>", "</strong>",
                "<strong>", "</strong>",
                "<u>", "</u>",
                "<em>", "</em>",
                "<em>", "</em>"
  return str_replace($bbcode, $htmlcode, $text);

You need to use <?php print bb2html($head_title); ?> in your page.tpl file.

For more reference, you can check this.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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