Is there a way to get the following code to display the link without spaces and capitals and add a dash in the space?

<?php
        foreach($node->taxonomy as $term){
          print '<a href="taxonomy/term/'.$term->name.'" rel="tag" title=""
          class="taxonomy_term_'.$term->tid .'">'.$term->name.'</a>';
       }
?>

Comments

justageek’s picture

Where is this code located?

To convert your term to all lowercase:

foreach($node->taxonomy as $term){
          print '<a href="taxonomy/term/'.$term->name.'" rel="tag" title=""
          class="taxonomy_term_'.$term->tid .'">'.strtolower($term->name).'</a>';
       }

To replace spaces with dashes you can do this:

foreach($node->taxonomy as $term){
          print '<a href="taxonomy/term/'.$term->name.'" rel="tag" title=""
          class="taxonomy_term_'.$term->tid .'">'.str_replace(' ','-',strtolower($term->name)).'</a>';
       }

But that will not work with multiple spaces, so you would need a regular expression, like this:

foreach($node->taxonomy as $term){
          print '<a href="taxonomy/term/'.$term->name.'" rel="tag" title=""
          class="taxonomy_term_'.$term->tid .'">'.preg_replace('/[\s]+/ ','-',strtolower($term->name)).'</a>';
}
BarisW’s picture