wordpress str_replace the content with tag

I can see that you are constructing an array with only one element, something like this:

array( 'green, yellow, red' )

instead of an array with multiple elements:

array( 'green', 'yellow', 'red' )

Try to replace these lines:

$out .= $tag->name .','; 
$csv_tags .= '"<a href="https://wordpress.stackexchange.com/" . $tag->slug . '">' . $tag->name . '</a>"';

with

$find[] = $tag->name;
$replace[] = '<a href="https://wordpress.stackexchange.com/" . $tag->slug . '">' . $tag->name . '</a>';

and remember to declare the arrays at the top:

$find = array();
$replace = array();