wp_get_attachment_image_attributes not working

I’ve replaced your string_replace call with preg_replace and converted your search string to proper regex (I use regex101 for that). Because you’re trying to replace images in the content, which wordpress saves as a string, you need to find anywhere in the content with an image and replace the src and srcset for it. This should work (although if you have any script tags with a src attribute in it then it would attempt to replace those as well. You could probably modify that regex to only match src and srcset within an image tag but that’s a bit more advanced than my regex skills will take us.

function alter_image_src_in_content($content)
{
    $content = preg_replace('~(?:(?:srcset="https://wordpress.stackexchange.com/questions/326569/)"(?:src="https://wordpress.stackexchange.com/questions/326569/)"(?:, ))(https:\/\/www\.example\.com\/wp-content\/uploads\/)~', 'https://sub.example.com/', $content);

    return $content;
}

add_filter('the_content', 'alter_image_src_in_content');

error code: 523