Taxonomy terms manipulation on node.tpl.php I wanted to print taxonomy terms on my custom node-mytype.tpl.php but what I want is not to print all the terms as they are.

Each of these terms consists of 2-3 words. And I want to manipulate the terms as below:

term1: word1 word2 word3

I want to print this as: 


  word3, word1 word2



If a node has one term it's easy and I can do this as below (of course there can be some other better way to do this):

inside node-mytype.tpl.php:

&lt;?php if (!empty($node-&gt;field_my_taxonomy_term)) { ?&gt;

    &lt;?php $aut = ($node-&gt;field_my_taxonomy_term['und']['0']['taxonomy_term']-&gt;name); ?&gt;
    &lt;?php $exp = explode(" ", $aut); ?&gt;     

    &lt;?php if (count($exp) == 2) {?&gt;
    &lt;?php print ($exp[1]); print t(", "); print ($exp[0]); print t(", "); ?&gt;

    &lt;?php } elseif (count($exp) == 3) {?&gt;
    &lt;?php print ($exp[2]); print t(", "); print ($exp[0]); print t(" "); print ($exp[1]); print t(", "); ?&gt;
    &lt;?php }?&gt;




I want to print multiple terms as above. For example:

term1: word1 word2 word3

term2: word4 word5 word6

should be printed as that:


  word3, word1 word2 - word6, word4 word5



I have this:

   &lt;?php $str = ''; ?&gt;
        &lt;?php if (isset($node-&gt;field_my_taxonomy_term[LANGUAGE_NONE]) &amp;&amp; $node-&gt;field_my_taxonomy_term[LANGUAGE_NONE]): ?&gt;

        &lt;?php foreach ($node-&gt;field_my_taxonomy_term[LANGUAGE_NONE] as $obj): ?&gt;
        &lt;?php $str .= l($obj['taxonomy_term']-&gt;name, 'taxonomy/term/' . $obj['tid']) . '&amp;nbsp;-&amp;nbsp;'; ?&gt;
        &lt;?php endforeach ?&gt;

        &lt;?php print trim($str, '-&amp;nbsp;'); ?&gt;
   &lt;?php endif ?&gt;



The below code prints the terms as that:


  word1 word2 word3 - word4 word5 word6



What code should I use here to print the terms as I wanted?


  word3, word1 word2 - word6, word4 word5


(Drupal 7.34)
Finally I did it as shown below (thanks SO):

  &lt;?php if (isset($node-&gt;field_my_taxonomy_term[LANGUAGE_NONE]) &amp;&amp; $node-&gt;field_my_taxonomy_term[LANGUAGE_NONE]): ?&gt;
    &lt;?php $allOf=''?&gt;
    &lt;?php foreach ($node-&gt;field_my_taxonomy_term[LANGUAGE_NONE] as $obj): ?&gt;
        &lt;?php $exp = explode (" ", $obj['taxonomy_term']-&gt;name);
            $exp_last = array_pop($exp);
            $eachOne = $exp_last .', ' .implode(' ', $exp);
            $allOf .= $eachOne . '&amp;nbsp;-&amp;nbsp;';
         ?&gt;
    &lt;?php endforeach; ?&gt;
    &lt;?php print trim($allOf, '&amp;nbsp;-&amp;nbsp;'); ?&gt;
  &lt;?php endif ?&gt;


This is working correctly.