Editing “Kahi’s Highlight Used Categories” plugin code – highlighting parent and child category in post page

Use the category ID:

class KHUC {
    function wp_list_categories ($text) {
        global $post;
        if (is_singular()) {
            $categories = wp_get_post_categories($post->ID);
            foreach ($categories as $category_id) {
                $category = get_category($category_id);
                $category_parent = get_category($category->category_parent);
                $text = preg_replace(
                    '#class="([^"]*cat-item-'.$category->cat_ID.'[^"0-9]*)"#',
                    'class="$1 used-cat"', 
                    $text);
                $text = preg_replace(
                    '#class="([^"]*cat-item-'.$category_parent->cat_ID.'[^"0-9]*)"#',
                    'class="$1 used-cat-parent"', 
                    $text);
            }
        }
        return $text;
    }
}
add_filter('wp_list_categories', array('KHUC', 'wp_list_categories'));

tech