How to filter part of a variable if it is no array?

The documentation is well explicable of the topic, let’s have a look here

In your specific case my_filter is applied no matter what, even if there is no value it. To apply the filter only if is_array() you could insert a specific instruction directly into your filter like:

function example_callback( $output ) {
        //do something and replace part of the string with str_replace()
    return $output;
add_filter( 'my_filter', 'example_callback', 10, 1 );

About the str_replace() php function read more here