Title: str_replace problem!
Last modified: August 20, 2016

---

# str_replace problem!

 *  Resolved [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/str_replace/)
 * I have a function to return a category menu, I am trying to add a ‘color’ class
   to the returned menu variable.
 * Lets say we have category $i = 1 and $style=’green’
 *     ```
       $cat_class = 'cat-item cat-item-' .$i;
       $amended_class = $cat_class .' ' .$style;
       $catlist = str_replace($cat_class, $amended_class, $catlist );
       ```
   
 * This works as expected as:
    `cat-item cat-item-1` will become `cat-item cat-item-
   1 green`
 * Thats great but it will replace all instances, so we then hit unlucky ’13’
    `
   cat-item cat-item-13` will become `cat-item cat-item-1 green3`
 * I have tried adding a space to the $cat_class:
    $cat_class = ‘cat-item cat-item-‘.
   $i .’ ‘;
 * But this does not work!
 * I am using the same code for category ancestors so it means that these will be
   doing the same and splitting the 13 if 1 and 13 are in the same tree.
 * Any ideas?
 * David

Viewing 1 replies (of 1 total)

 *  Thread Starter [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/str_replace/#post-2123770)
 * Ok figured it out!
 *     ```
       if( $style ) {
       	/* Normal Category Menu Item */
       	$cat_class = '"cat-item cat-item-' .$i .'"';
       	$amended_class = '"cat-item cat-item-' .$i .' ' .$style .'"';
       	$catlist = str_replace($cat_class, $amended_class, $catlist );
       	/* Parent Category Menu Item */
       	$cat_class = '"cat-item cat-item-' .$i .' current-cat-parent"';
       	$amended_class = '"cat-item cat-item-' .$i .'  current-cat-parent ' .$style .'"';
       	$catlist = str_replace($cat_class, $amended_class, $catlist );
       	/* Ancestor Category Menu Item */
       	$cat_class = '"cat-item cat-item-' .$i .' current-cat-ancestor"';
       	$amended_class = '"cat-item cat-item-' .$i .'  current-cat-ancestor ' .$style .'"';
       	$catlist = str_replace($cat_class, $amended_class, $catlist );
       }
       ```
   
 * This may help someone else!
 * David

Viewing 1 replies (of 1 total)

The topic ‘str_replace problem!’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/str_replace/#post-2123770)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
